手机
当前位置:查字典教程网 >编程开发 >C#教程 >轻松学习C#的String类
轻松学习C#的String类
摘要:字符串是由零个或多个字符组成的有限序列,是几乎所有编程语言中可以实现的非常重要和有用的数据类型。在C#语言中,字符串是System.Stri...

字符串是由零个或多个字符组成的有限序列,是几乎所有编程语言中可以实现的非常重要和有用的数据类型。在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>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>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>

输出的结果为:

轻松学习C#的String类1

三、字符串的合并

字符串的合并通过“+”,Concat方法和Join方法来实现的。

(1)用“+”符号来连接两个字符串后形成一个新的字符串,格式为:字符串1+字符串2=字符串3。

(2)用Concat方法连接字符串的格式为:string.Concat(字符串1,字符串2,...,字符串n)。

(3)用Join方法是将字符串数据合并为一个新的字符串,格式为:string.Join(合并后的分隔符,字符串数组)。

例三,实现对字符串str1和str2的合并

<span>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>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>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>

输出的结果为:

轻松学习C#的String类2

六,字符串的删除

字符串的删除是通过Remove方法实现的,格式为:

(1)字符串.Remove(开始位置)

(2)字符串.Remove(开始位置,移除数)

其中,开始位置是指字符串的索引,是一个整数,且小于字符串的长度。第一种格式,是将字符串开始位置后的所有子子符删除,而第二种是将从开始位置开始数到移除数位置的字符删除。

例六,实现字符串str的删除

<span>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>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>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>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>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>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:Windowssystem32");//第一种输出格式就是在前面加@ Console.WriteLine("C:Windowssystem32");//第二种输出格式就是将""改成"" Console.ReadLine(); } } } </span>

输出的结果为:

轻松学习C#的String类3

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

【轻松学习C#的String类】相关文章:

C#数组学习相关资料整理

winform中写app.config文件时调试情况下没有改变的原因

C#几种截取字符串的方法小结

C#多维数组学习使用

C#简单的加密类实例

探讨C#中Dispose方法与Close方法的区别详解

C#软件注册码的实现代码

C# 判断字符串为空的几种办法

学习C#静态函数及变量的一个精典例子与代码

遍历Hashtable 的几种方法

精品推荐
分类导航