1.字符串
1.1程序代码
static void Main(string[] args)
{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);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;Console.WriteLine("\n{0}\n{1}", sentence1, sentence2);string sentence3 = "再多看一眼";string sentence4 = string.Copy(sentence3);Console.WriteLine(sentence3);Console.WriteLine(sentence4);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]);}string string5 = "Hello";string string6 = "hello";if (string5 == string6){Console.WriteLine("\nEqual");}else{Console.WriteLine("\nUnequal");}string string7 = "Hello";if (string7.Equals("Hello")){Console.WriteLine("\nEqual");}else{Console.WriteLine("\nUnequal");}string string8 = "Dog";string string9 = "dog";if (string.Equals(string8, string9)){Console.WriteLine("\nEqual");}else{Console.WriteLine("\nUnequal");}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)
{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]);}}for (int i = 0; i < strings.Length; i++){if (strings[i].EndsWith("ed")){Console.WriteLine("\"{0}\" starts with \"ed\"", strings[i]);}}string 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);int index3 = sentence.IndexOf('o', 6);int index4 = sentence.IndexOf("You", 20,4);Console.WriteLine(index3);Console.WriteLine(index4);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);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);string sub=sentence.Substring(12);Console.WriteLine(sub);string sub1=sentence.Substring(5,4);Console.WriteLine(sub1);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);Console.WriteLine(string8);string string9=string4.Remove(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);Console.WriteLine(result);
}