当前位置: 移动技术网 > IT编程>开发语言>c# > 轻松学习C#的String类

轻松学习C#的String类

2019年07月18日  | 移动技术网IT编程  | 我要评论
字符串是由零个或多个字符组成的有限序列,是几乎所有编程语言中可以实现的非常重要和有用的数据类型。在c#语言中,字符串是system.string类的一个引用类型,但与其他引

字符串是由零个或多个字符组成的有限序列,是几乎所有编程语言中可以实现的非常重要和有用的数据类型。在c#语言中,字符串是system.string类的一个引用类型,但与其他引用类型不同的是,c#将字符串视为一个基本类型,可以声明为一个常量,并可以直接赋值。由于c#中的字符串是由system,string类派生而来的引用对象,因此可以使用string类的方法来对字符串进行各种操作。下面通过几个例子来讲述string类的几个重要方法。
一、字符串的截取
字符串截取是通过substring方法实现的,它有两种重载方法,格式分别为:
         (1)字符串1.substring(整数n);将字符串1前n个长度的字符串截取掉,保留后面的字符串
         (2)字符串1.substring(整数n,整数m);保留从字符串1第n个长度开始数m个长度的字符串
         两种重载方法都返回一个新的字符串。
例一:实现对字符串“0123456789”的截取

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   string nums = "0123456789"; 
   string newnums1; 
   string newnums2; 
   newnums1 = nums.substring(5);//截取从索引5开始后的字符 
   newnums2 = nums.substring(3,5);//截取从索引3开始数5个字符 
   console.writeline(newnums1); 
   console.writeline(newnums2); 
   console.readline(); 
  } 
 } 
}</span> 

输出的结果为:56789
                       34567
注意:字符串的索引是从0开始的,在使用substring方法的第二种重载时,整数n和整数m的和不要大于要截取的字符串的长度,否则会产生越出索引异常。
二、字符串的分割
字符串的分割是通过split方法实现的。常用的一种格式为:
        字符串1.split(字符串或字符数组)
        通过split方法分割字符串后将生成多个字符串,所以经过split方法分割的返回值是一个字符串数组。
例二:实现对字符串“abcefgaabsbdeesdabc”的分割

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   string str="abcefgaabsbdeesdabc"; 
   console.writeline("原字符串为{0}",str); 
   console.writeline("通过单个字符e分割后如下:"); 
   string[] singlesplit = str.split('e');//进行单个字符分割的方法 
   foreach (string outstr in singlesplit) 
   { 
    console.writeline(outstr); 
   } 
   console.writeline("通过多个字符e,b,s分割后如下:"); 
   string[] multisplit = str.split(new char[] {'e','b','s'});//进行多个字符分割的方法 
   foreach (string outstr in multisplit) 
   { 
    console.writeline(outstr); 
   } 
   console.readline(); 
  } 
 } 
} 
</span> 

输出的结果为:

三、字符串的合并
字符串的合并通过“+”,concat方法和join方法来实现的。
         (1)用“+”符号来连接两个字符串后形成一个新的字符串,格式为:字符串1+字符串2=字符串3。
         (2)用concat方法连接字符串的格式为:string.concat(字符串1,字符串2,...,字符串n)。
         (3)用join方法是将字符串数据合并为一个新的字符串,格式为:string.join(合并后的分隔符,字符串数组)。
例三,实现对字符串str1和str2的合并

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   string str1 = "abc"; 
   string str2 = "ghj"; 
   string[] array = {"123","456","789"}; 
   string str3 = str1 + str2; 
   string str4 = string.concat(str1,str2); 
   string str5 = string.join("|",array);//将数组中元素合并为一个新的字符串 
   console.writeline(str3); 
   console.writeline(str4); 
   console.writeline(str5); 
   console.readline(); 
  } 
 } 
}</span> 

输出的结果为:abcghj
                       abcghj
                       123|456|789
四、字符串的替换
       字符串的替换是通过replace方法实现的,格式为:字符串.replace(要替换的原字符串,替换后的字符串);
例四,实现对字符串str的替换

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   string str = "abcadfaslfj"; 
   string replacestr = str.replace("a","|");//用“|”替换“a” 
   console.writeline(replacestr); 
   console.readline(); 
  } 
 } 
}</span> 

输出的结果为:|bc|df|slfj
五、字符串的插入与填充
        字符串的插入是通过insert方法实现的,其格式为:字符串.insert(插入位置,插入字串)
        字符串的填充是通过padright方法和padleft方法实现的。
        padright方法是在字符串的结尾通过添加指定的重复字符填充字符串,格式为:字符串.padright(总长度)(以空格填充)和字符串.padright(总长度,要填充的字符)。
        padleft方法是在字符串的开头通过添加指定的重复字符填充字符串,格式为:字符串.padleft(总长度)(以空格填充)和字符串.padleft(总长度,要填充的字符)。
例五、实现对字符串str的插入与填充

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   string str = "abcdefg"; 
   string insertstr; 
   string padrightstr; 
   string padleftstr; 
   insertstr = str.insert(5,"12345"); 
   padrightstr = str.padright(10,'v'); 
   padleftstr = str.padleft(10,'w'); 
   console.writeline(insertstr); 
   console.writeline(padrightstr); 
   console.writeline(padleftstr); 
   console.readline(); 
  } 
 } 
}</span> 

输出的结果为:

 六,字符串的删除
字符串的删除是通过remove方法实现的,格式为:
         (1)字符串.remove(开始位置)
         (2)字符串.remove(开始位置,移除数)
其中,开始位置是指字符串的索引,是一个整数,且小于字符串的长度。第一种格式,是将字符串开始位置后的所有子子符删除,而第二种是将从开始位置开始数到移除数位置的字符删除。
例六,实现字符串str的删除

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
 static void main(string[] args) 
 { 
  string str = "0123456789"; 
  string delstr1; 
  string delstr2; 
  delstr1 = str.remove(6);//删除字符串索引为6后面的字符 
  delstr2 = str.remove(5,5);//删除字符串索引自5开始后数5个长度的字符 
  console.writeline(delstr1); 
  console.writeline(delstr2); 
  console.readline(); 
 } 
 } 
}</span> 

输出的结果为:012345
                       01234
七、字符串的复制
         字符串的复制是通过copy方法和copyto方法实现的。若想把一个字符串复制到另一个字符数组中,可以使用string的静态方法copy来实现。其格式为:string.copy(要复制的字符串)。
         copyto方法可以实现copy同样的功能,但是功能更为丰富,可以复制原字符串的一部分到一个字符数组中,其格式为:copyto(要复制的字符起始位置,目标字符数组,目标数组中的开始存放位置,要复制的字符个数)。
例七,实现字符串str的复制

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
 static void main(string[] args) 
 { 
  string str = "this is a string"; 
  string copystr; 
  copystr = string.copy(str); 
  char[] newchar=new char[20]; 
  str.copyto(5,newchar,0,11); 
  console.writeline(copystr); 
  console.writeline(newchar); 
  console.readline(); 
 } 
 } 
}</span> 

输出的结果为:this is a string
                        is a string
八、字符串的大小写转换
         字符串大小写转换是通过string类的tolower方法和toupper方法实现的,tolower方法是将字符串转换为小写形式,toupper是将字符串转换为大写形式。
例八,实现字符串str的大小写转换

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
 static void main(string[] args) 
 { 
  string str = "this is a string"; 
  string lowerstr; 
  string upperstr; 
  lowerstr = str.tolower(); 
  upperstr = str.toupper(); 
  console.writeline("小写形式:{0}",lowerstr); 
  console.writeline("大写形式:{0}",upperstr); 
  console.readline(); 
 } 
 } 
}</span> 

输出的结果为:this is a string
                       this is a string
九、字符串的查找
         字符串的查找是通过indexof方法和lastindexof方法实现的。其格式为:
         字符串.indexof(要查找的字符或字符串)
         字符串.lastindexof(要查找的字符或字符串)
         其中,indexof方法是返回要查找的字符或字符串第一次在所要查找的字符串出现的位置,lastindexof方法是返回要查找的字符或字符串最后一次在所要查找的字符串中出现的位置。indexof方法和lastindexof方法都返回一个整数,如果在所要查找的字符串内不包含要查找的字符或字符串则返回一个负数。
例九,实现字符串str的查找

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
 static void main(string[] args) 
 { 
  string str = "this is a string"; 
  int rh1 = str.indexof("i"); 
  int rh2 = str.lastindexof("i"); 
  if (rh1>=0) 
  { 
  console.writeline("字符i在字符串str第一次出现的位置是:{0}",rh1); 
  console.writeline("字符i在字符串str最后一次出现的位置是:{0}", rh2); 
  } 
  else 
  { 
  console.writeline("字符i在字符串str未出现"); 
  } 
  console.readline(); 
 } 
 } 
}</span> 

输出的结果为:字符i在字符串str第一次出现的位置是:2
                       字符i在字符串str最后一次出现的位置是:13
十、字符串的追加
        在使用system.string类中的方法时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间。在需要对字符串执行重复修改的情况下,与创建新的string对象相关的系统开销就可能非常高。为了解决这个问题,c#提供了一个类stringbuilder。
        使用stringbuilder类时,首先要引入system.text命名空间,然后通过new关键字对其进行初始化。stringbuilder类的方法使用和string类的方法使用是一样的。
例十、实现对字符串str的追加

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
 static void main(string[] args) 
 { 
  stringbuilder str =new stringbuilder( "hellow world!"); 
  console.writeline("---append---"); 
  str.append("what a beautiful day"); 
  console.writeline("追加后的字符串为:{0}",str); 
  console.readline(); 
 } 
 } 
}</span> 

输出的结果为:---append---
                       追加后的字符串为:hellow world! what a beautiful day
补充:转义字符
         转义字符具有特定的含义,不同于字符原有的意义的字符。在c#语言中,转义字符是指“\”,主要用来表示那些用一般字符不方便表示的控制代码。
        对于转义字符的输出c#语言有着特殊的格式:

<span style="font-size:18px;">using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 字符串 
{ 
 class program 
 { 
 static void main(string[] args) 
 { 
  console.writeline(@"c:\windows\system32");//第一种输出格式就是在前面加@ 
  console.writeline("c:\\windows\\system32");//第二种输出格式就是将"\"改成"\\" 
  console.readline(); 
 } 
 } 
} 
</span> 

输出的结果为:

以上就是c#的string类全部学习例题,很详细的学习教程,希望对大家的学习有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网