1.将字符串的某个字符赋值给char变量
eg:string myString="A string";
char myChar=myString[1];
2.将字符串的字符赋给char数组
eg:string myString="A String";
char [] myChars=myString.ToCharArray();
3.用foreach逐个输出string的char字符
4.可以像数组一样,用myString.Length获取元素的个数
5.将字符串中的字符改变大 写
1).大写变小写:myString.ToLower()
2).小写变大写:myString.ToUpper();
6.删除字符串中的空格:myString.Trim();
7.删除字符串中的其他字符
第一步:在char数组中存储要删除的字符
第二步:使用.Trim()命令
eg:char [] trimChar={' ',',','e','s'};
string afterChangeString=myString.Trim(trimChar);
8.删除字符串前面或后面的字符
1).前面:myString.TrimStart();
2).后面:myString.TrimEnd();
9.在字符串的左面或后面添加空格使字符串达到指定的长度
1).左面:myString.PadLeft(number);
2).右面:myString.PadRight(number);
10.用指定的分隔符分隔字符串
eg:char [] separator={' '};
string [] mywords;
mywords=myString.Split(separator);
失误案例:
string myString = " My First String ";
string deleteBlockString = myString.Trim(); //删除字符串的空格
char[] deleteCharArray={' ','i'};
string deleteDirectlyCharString = myString.Trim(deleteCharArray); //删除字符串中的空格和字符i
string deleteStartBlockString = myString.TrimStart(); //删除字符串开头的空格
string deleteEndBlockString = myString.TrimEnd(); //删除字符串结尾的空格
Console.WriteLine("The string after delete Block is \"{0}\"", deleteBlockString);
Console.WriteLine("The string after delete directly is \"{0}\"", deleteDirectlyCharString);
Console.WriteLine("The string after delete start block is \"{0}\"", deleteStartBlockString);
Console.WriteLine("The string after delete end block is \"{0}\"", deleteEndBlockString);
Console.WriteLine("The string after delte Block is \"{0}\".\nThe string after delete directly Charracters is \"{1}\".\n",myString.Trim(),myString.Trim(deleteCharArray));
Console.ReadKey();