字符串可以当做字符char类型的数组,因此我们可以通过数组遍历字符串的每一个字符。
name[index]访问指定字符串指定索引位置处的字符;str.Length返回字符串的长度(有多少个字符)
练习:对用户输入的字符串进行反向的输出显示
string str=Console.ReadLine(); for (int i = str.Length-1; i >=0 ; i--) { Console.Write(str[i]); } Console.ReadKey();
关于字符串的一些方法(对原字符串无影响):
1,ToLower() ToUpper()
2,Trim() TrimStart() TrimEnd()
3,Split():把字符串按指定字符拆分,返回拆分后的字符串数组:
string str = "www.taikr.COM"; string[] strArray = str.Split('.'); foreach (string temp in strArray) { Console.WriteLine(temp); } Console.ReadKey();