C#字符串的操作及方法归纳(三)
-
字符串的裁剪
public String Trim(); //去掉字符串首尾默认字符(空格)
public String Trim(params char[] trimChars); //去掉字符串首尾的自定义字符
public String TrimStart(params char[] trimChars); //去掉字符串首部默认字符(空格)
public String TrimEnd(params char[] trimChars); //去掉字符串尾部默认字符(空格)
string str = " HelloWorld "; Console.WriteLine(str.Trim());//输出:HelloWorld Console.WriteLine(str.TrimStart());//输出:HelloWorld (光标处) Console.WriteLine(str.TrimEnd());//输出: HelloWorld
string str = "123Hello3World1253124"; Console.WriteLine(str.Trim('1', '2', '3', '4'));//Hello3World125
注意:裁剪字符串时,只有相连的部分才会被去除。
-
字符串的复制
1、Copy
public static String Copy(String str); //复制字符串str,返回新字符串
string str1 = "Hello World"; string str2 = string.Copy(str1); Console.WriteLine(str2);//输出:Hello World
2、CopyTo
//将字符串从下标为sourceIndex开始(不包括sourceIndex)复制到字符数组destination中,从字符数组destination的下标为destinationIndex依次复制count个
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);string str1 = "Hello World"; char[] chs = new char[6]; str1.CopyTo(2, chs, 0, 6); foreach (char c in chs) { Console.Write(c);//输出:llo Wo }
注意:要保证字符数组有足够的存储位置,同时要保证复制的字符串下标(sourceIndex) + 复制内容的长度(count) <= 原字符串长度
-
字符串的替换
public String Replace(String oldValue, String newValue); //将字符串的字符串oldValue替换成字符串newValue
public String Replace(char oldChar, char newChar); //将字符串的字符oldChar替换成字符newChar
string str = "abcdefg"; Console.WriteLine(str.Replace("bc", "HH"));//输出:aHHdefg Console.WriteLine(str.Replace('b', 'B'));//输出:aBcdefg
-
字符串的查找
1、从前往后找(IndexOf)
//查找字符value第一次出现在字符串的索引位置
public int IndexOf(char value);//查找字符串value第一次出现在字符串的索引位置
public int IndexOf(String value);//从字符串下标为startIndex开始(包括startIndex)查找字符value第一次出现在字符串的索引位置
public int IndexOf(char value, int startIndex);string str = "abcdabcdabcd"; Console.WriteLine(str.IndexOf('c'));//输出:2 Console.WriteLine(str.IndexOf("cdab"));//输出:2 Console.WriteLine(str.IndexOf('c', 6));//输出:6
2、从后往前找(LastIndexOf)
public int LastIndexOf(char value); //同【IndexOf】查找字符value,但查找顺序改变
public int LastIndexOf(String value); //同【IndexOf】查找字符串value,但查找顺序改变
public int LastIndexOf(String value, int startIndex); //同【IndexOf】查找字符value,但查找顺序改变
string str = "abcdabcdabcd"; Console.WriteLine(str.LastIndexOf('c'));//输出:10 Console.WriteLine(str.LastIndexOf("cdab"));//输出:6 Console.WriteLine(str.LastIndexOf('c', 6));//输出:6
-
字符串的判断
1、StartsWith
public bool StartsWith(String value); //判断字符串是否以字符串value开头,返回True或False
string str = " HELLO WORLD"; if (str.StartsWith(" H")) { Console.WriteLine("字符串以 H开头");//输出 }
注意:字符串本身也可以作为字符串的开头。
2、EndsWith
public bool EndsWith(String value); //判断字符串是否以字符串value结尾,返回True或False
string str = " HELLO WORLD"; if (str.EndsWith(" HELLO WORLD")) { Console.WriteLine("字符串以 HELLO WORLD结尾");//输出 }
注意:字符串本身也可以作为字符串的结尾。
3、Contains
public bool Contains(String value); //判断字符串是否包含字符串value,返回True或False
string str = " HELLO WORLD"; if (str.Contains("HELLO")) { Console.WriteLine("字符串包含HELLO字符串");//输出 }
4、IsNullOrEmpty
public static bool IsNullOrEmpty(String value); //判断字符串是否为null或空,返回True或False
string str1 = null; string str2 = ""; if (string.IsNullOrEmpty(str1) && string.IsNullOrEmpty(str2)) { Console.WriteLine("字符串是否为null或空");//输出 }
-
字符转化为字符串
public String(char[] value); //将字符数组转化为字符串
char[] chs = { 'a', 'b', 'c', 'd' }; string str = new string(chs); Console.WriteLine(str);//输出:abcd
因为作者精力有限,文章中难免出现一些错漏,敬请广大专家和网友批评、指正。