欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > zy89、90_C#中字符串及控制字符串的常用函数

zy89、90_C#中字符串及控制字符串的常用函数

2024/11/30 14:40:00 来源:https://blog.csdn.net/weixin_47149785/article/details/142637523  浏览:    关键词:zy89、90_C#中字符串及控制字符串的常用函数

文章目录

  • 1.字符串
    • 1.1程序代码
  • 2.控制字符串的常用函数
    • 2.1程序代码

1.字符串

1.1程序代码

static void Main(string[] args)
{//********************string类*******************string sentence = "Hello!";char letter = sentence[0];Console.WriteLine(letter);char[] letter1 = { 'C', 'H', 'I', 'C', 'K', 'E', 'N' };string ss = new string(letter1);Console.WriteLine(ss);string ss1 = new string('A', 10);//重复执行10遍Console.WriteLine(ss1);Console.WriteLine("The letters in \"Hello\" are:");for (int i = 0; i < sentence.Length; i++){char letter2 = sentence[i];Console.Write(letter2 + "\t");}//********************复制字符串*******************// =:string sentence1 = "就快被融化";string sentence2 = sentence1;//sentence1 = "再靠近一点";Console.WriteLine("\n{0}\n{1}", sentence1, sentence2);//Copy()string sentence3 = "再多看一眼";string sentence4 = string.Copy(sentence3);Console.WriteLine(sentence3);Console.WriteLine(sentence4);//CopyTo()string source = "someone";char[] destination = { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' };int sourceIndex = 2;int destinationIndex = 3;int count = 2;source.CopyTo(sourceIndex, destination, destinationIndex, count);for (int i = 0; i < destination.Length; i++){Console.Write(destination[i]);}//********************比较字符串*******************//1.相等运算符==string string5 = "Hello";string string6 = "hello";if (string5 == string6){Console.WriteLine("\nEqual");}else{Console.WriteLine("\nUnequal");}//2.Equsls()string string7 = "Hello";if (string7.Equals("Hello")){Console.WriteLine("\nEqual");}else{Console.WriteLine("\nUnequal");}//3.静态Equal()string string8 = "Dog";string string9 = "dog";if (string.Equals(string8, string9)){Console.WriteLine("\nEqual");}else{Console.WriteLine("\nUnequal");}//4.CompareTo()string string10 = "better";string string11 = "best";switch (string10.CompareTo(string11)){case -1: Console.WriteLine(string10 + "<" + string11); break;case 0: Console.WriteLine(string10 + "=" + string11); break;case 1: Console.WriteLine(string10 + ">" + string11); break;default: Console.WriteLine("********"); break;}
}

2.控制字符串的常用函数

2.1程序代码

static void Main(string[] args)
{验证字符串的首尾//1.StartWith()判断字符串是否以特定的子串开头string[] strings = { "started", "starting", "ended", "ending" };for (int i = 0; i < strings.Length; i++){if (strings[i].StartsWith("st")){Console.WriteLine("\"{0}\" starts with \"st\"", strings[i]);}}//2.EndsWith()判断字符串是否以特定的子串结尾for (int i = 0; i < strings.Length; i++){if (strings[i].EndsWith("ed")){Console.WriteLine("\"{0}\" starts with \"ed\"", strings[i]);}}定位字符或子串//1.IndexOf()找到指定字符或子串在字符串中第一次出现的位置。如果找不到,返回-1string sentence = "You love to hear Let her go.";int index1 = sentence.IndexOf('o', 3);//从第三个字符之后开始找指定元素int index2 = sentence.IndexOf("You", 20);Console.WriteLine(index1);Console.WriteLine(index2);//2.LastIndexOf()找到指定字符或子串在字符串中最后一次出现的位置。如果找不到,返回-1int index3 = sentence.IndexOf('o', 6);//从后向前搜索int index4 = sentence.IndexOf("You", 20,4);//向前搜索4个字符Console.WriteLine(index3);Console.WriteLine(index4);//3.IndexOfAny()同时搜索多个指定的字符,搜索到任意一个停止char[] searchLetters= { 'e', 'h' };int index5 = sentence.IndexOfAny(searchLetters);int index6 = sentence.IndexOfAny(searchLetters,10);//第十个元素开始int index7 = sentence.IndexOfAny(searchLetters,10,3);Console.WriteLine(index5);Console.WriteLine(index6);Console.WriteLine(index7);//4.LastIndexOfAny()int index8 = sentence.LastIndexOfAny(searchLetters);int index9 = sentence.LastIndexOfAny(searchLetters, 10);int index10 = sentence.LastIndexOfAny(searchLetters, 10, 3);Console.WriteLine(index8);Console.WriteLine(index9);Console.WriteLine(index10);///截取子串Substring()string sub=sentence.Substring(12);//截取12之后的字符串Console.WriteLine(sub);string sub1=sentence.Substring(5,4);//在第5个之后截取4个Console.WriteLine(sub1);//拆分字符串Split()char[] seperator = { ' ','o' };//遇到字符断开string[] words=sentence.Split(seperator);foreach (string word in words){Console.WriteLine(word);}//更改大小写string string1 = "ok";string string2=string1.ToUpper();Console.WriteLine(string2);string string3 = string2.ToLower();Console.WriteLine(string3);//修改字符串string string4 = "tree";string string5 = string4.Insert(1, "h");Console.WriteLine(string5);string string6 = string4.Replace('e', 'd');Console.WriteLine(string6);string string7 = string4.Replace("re", "ffd");Console.WriteLine(string7);string string8 = string4.Remove(2);//2以后所有元素Console.WriteLine(string8);string string9=string4.Remove(1,2);//1以后的2个字符Console.WriteLine(string9);插入格式化变量int i1 = 360;int j1 = 60;string result = string.Format("{0,6}\n+{1,5}\n----------\n{2,6}", i1, j1, i1 + j1);//{0}6个空间Console.WriteLine(result);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com