码迷,mamicode.com
首页 > Windows程序 > 详细

C#拾遗之String类(二)

时间:2015-03-31 22:28:56      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:c#   string   stringbuilder   visual studio   .net framework   

         接上一篇文章继续说String类

         六,字符串的删除

         字符串的删除是通过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类(二)

标签:c#   string   stringbuilder   visual studio   .net framework   

原文地址:http://blog.csdn.net/erlian1992/article/details/44783741

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!