C#拾遗之String类(一)

来源:互联网 发布:淘宝追加好评长心吗 编辑:程序博客网 时间:2024/06/10 02:36

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

         输出的结果为:

 

          

        

 


 

 

 


 


 

 

 

             

 


           

 

         

      

1 0
原创粉丝点击