国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

輕松學習C#的String類

2020-01-24 01:22:20
字體:
來源:轉載
供稿:網友

字符串是由零個或多個字符組成的有限序列,是幾乎所有編程語言中可以實現的非常重要和有用的數據類型。在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");//第二種輸出格式就是將"/"改成"http://"   Console.ReadLine();  }  } } </span> 

輸出的結果為:

以上就是C#的String類全部學習例題,很詳細的學習教程,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肥东县| 华亭县| 博野县| 施秉县| 密云县| 绥滨县| 澄城县| 湘潭县| 温州市| 浪卡子县| 清镇市| 襄垣县| 淅川县| 九龙县| 方正县| 双桥区| 陆丰市| 开鲁县| 崇阳县| 江油市| 个旧市| 伊春市| 攀枝花市| 崇州市| 肥东县| 金塔县| 容城县| 南城县| 舟曲县| 中阳县| 岳阳县| 芜湖市| 德兴市| 鄂托克旗| 临沭县| 东山县| 于田县| 通许县| 晋州市| 孟州市| 鄂州市|