有两种创建字符串的方法,第一章是直接赋值
string s1="this is a string.";另一种是通过构造函数创建字符串类型的对象
string s2=new string('a',4);如果要得到字符串中的某个字符,用中括号指明字符在字符串的序号即可
char chFirst=s1[2];//求字符串的第2个字符,结果是istring是Unicode字符串,即每个英文字母占两个字节,每个汉字也是两个字节,在计算字符串长度的时候,每个英文字母的长度是1,每股汉子的长度也是1
string str="ab李四cde"; Console.WriteLine(str.Length); //7要精确比较两个字符串的大小,可以使用string.Compare(string s1,string s2),返回三种结果 s1>s2,结果1 s1=s2,结果0 s1<s2,结果-1 另外,string.Compare(string s1,string s2,bool ignoreCase)在比较两个字符串大小时还可以指定是否区分大小写 如果仅比较两个字符串是否相等,最好用Equals方法或者直接使用“==”来比较
Con**加粗样式**sole.WriteLine(s1.Equals(s2)); Console.WriteLine(s1==s2);第一种方法:**直接使用string[index]**得到字符串中第index个位置的单个字符 第二种方法:Contains方法 查找一个字符串中是否包含了指定的子字符串
public bool Contains(string value)例如:
if(s1.Contains("abc"))Console.WriteLine("s1中包含abc");第三种方法:indexOf方法和LastIndexOf方法 IndexOf方法用于求某个字符或者子串在字符串中出现的位置,该方法有多种重载形式,最常用的有如下两种 (1)public int IndexOf(string s) 这种形式返回s在字符串中首次出现的从零开始的位置,如果字符串中不存在s,则返回-1。 (2)public int IndexOf(string s,init startIndex) 这种形式从startIndex处开始查找s在字符串中首次出现的从零开始的位置。如果找不到,则返回-1 LastIndexOf方法的用法与IndexOf方法相同,区别是LastIndexOf方法与IndexOf方法的查找方向刚好相反。 第四种方法:IndexOfAny方法 用于查找某个字符串中是否包含了某些字符(多个不同的字符)
public int IndexOfAny(char [] anyOf)该方法返回Unicode字符数组中的任意字符在字符串实例中第一个匹配项的从零开始的索引位置,如果未找到anyOf中的任何一个字符,则返回-1
希望得到一个字符串中从某个位置开始的子字符串,可以用Substring方法,例如:
string s1="abc123"; string s2=s1.Substring(2); //从第2个字符开始取到字符串末尾,结果是"c123" string s3=s1.Substring(2,3); //从第2个字符开始取3个字符,结果是"c12"从startIndex开始插入子字符串value
public string Insert(int startIndex,string value)删除从startIndex到字符串结尾的子字符串
public string Remove(int startIndex)删除从startIndex开始的count个字符
public string Remove(int startIndex,int count)将oldChar的所有匹配项均换成newChar
public string Replace(char oldChar,char new Char)将oldValue的所有匹配项均替换成newValue
public string Replace(char oldValue,string newValue)利用TrimStart方法可以移除字符串首部的一个或者多个字符 利用TrimEnd方法可以移除字符串尾部的一个或者多个字符串 利用Trim可以同时移除字符串首部和尾部的一个或者多个字符
string s1=" this is a book"; string s2="that is a pen "; string s3=" is a pen "; Console.WriteLine(s1.TrimStart()); //移除首部空格 Console.WriteLine(s2.TrimEnd()); //移除尾部空格 Console.WriteLine(s3.trim()); //移除首部和尾部空格 string str1="Hello World!"; char [] c={'r','o','w','l','d','!',' '}; string newStr=str1.TrimEnd(c); //移除str1尾部在字符数组c中包含的所有字符 string str2="北京北奥运会北京“; char [] c1={'北','京'}; string newStr2=str2.trim(c1); //“奥运会”用于数组的每个元素之间串联指定的分隔符,从而产生单个串联的字符串
public static string Join(string separator,string [] value)用于将字符串按照指定的一个或者多个字符进行分离,从而得到一个字符串数组,常用语法为
public string [] Split (params char [] separator)分割的字符参数个数可以是一个,也可以是多个。如果分隔符是多个字符,各字符之间用逗号分开。当有多个参数时,它表示只要找到其中任何一个分隔符,就将其分离。
string [] aArray1={"123","456","abc"}; string s1=string.Join(",",sArray1); //结果是“123,456,abc" string [] sArray2=s1.Split(','); string s2="abc 12;34,56"; string [] sArray3=s2.Split(',',';',' ');//分隔符为逗号、分号、空格 Console.WriteLine(string.Join(Environment.NewLine,sArray3)); /*结果: abc 12 34 56 */将字符串的所有英文字母转换成大写可以ongoingToUpper方法,将字符串的所有英文字母转换为小写可以用ToLower方法
string s1="this is a string"; string s2=s1.ToUpper();//THIS IS A STRING string s3=Console.ReadLine(); if(s3.ToLower()=="yes") { Console.WriteLine("OK"); }