輕松學(xué)習(xí)C#的String類
字符串是由零個(gè)或多個(gè)字符組成的有限序列,是幾乎所有編程語言中可以實(shí)現(xiàn)的非常重要和有用的數(shù)據(jù)類型。在C#語言中,字符串是System.String類的一個(gè)引用類型,但與其他引用類型不同的是,C#將字符串視為一個(gè)基本類型,可以聲明為一個(gè)常量,并可以直接賦值。由于C#中的字符串是由System,String類派生而來的引用對象,因此可以使用String類的方法來對字符串進(jìn)行各種操作。下面通過幾個(gè)例子來講述String類的幾個(gè)重要方法。
一、字符串的截取
字符串截取是通過Substring方法實(shí)現(xiàn)的,它有兩種重載方法,格式分別為:
(1)字符串1.Substring(整數(shù)n);將字符串1前n個(gè)長度的字符串截取掉,保留后面的字符串
(2)字符串1.Substring(整數(shù)n,整數(shù)m);保留從字符串1第n個(gè)長度開始數(shù)m個(gè)長度的字符串
兩種重載方法都返回一個(gè)新的字符串。
例一:實(shí)現(xiàn)對字符串“0123456789”的截取
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開始數(shù)5個(gè)字符 Console.WriteLine(newnums1); Console.WriteLine(newnums2); Console.ReadLine(); } } }
輸出的結(jié)果為:56789
34567
注意:字符串的索引是從0開始的,在使用Substring方法的第二種重載時(shí),整數(shù)n和整數(shù)m的和不要大于要截取的字符串的長度,否則會(huì)產(chǎn)生越出索引異常。
二、字符串的分割
字符串的分割是通過Split方法實(shí)現(xiàn)的。常用的一種格式為:
字符串1.Split(字符串或字符數(shù)組)
通過Split方法分割字符串后將生成多個(gè)字符串,所以經(jīng)過Split方法分割的返回值是一個(gè)字符串?dāng)?shù)組。
例二:實(shí)現(xiàn)對字符串“abcefgaabsbdeesdabc”的分割
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("通過單個(gè)字符e分割后如下:"); string[] singleSplit = str.Split('e');//進(jìn)行單個(gè)字符分割的方法 foreach (string outstr in singleSplit) { Console.WriteLine(outstr); } Console.WriteLine("通過多個(gè)字符e,b,s分割后如下:"); string[] multiSplit = str.Split(new char[] {'e','b','s'});//進(jìn)行多個(gè)字符分割的方法 foreach (string outstr in multiSplit) { Console.WriteLine(outstr); } Console.ReadLine(); } } }
輸出的結(jié)果為:
三、字符串的合并
字符串的合并通過“+”,Concat方法和Join方法來實(shí)現(xiàn)的。
(1)用“+”符號來連接兩個(gè)字符串后形成一個(gè)新的字符串,格式為:字符串1+字符串2=字符串3。
(2)用Concat方法連接字符串的格式為:string.Concat(字符串1,字符串2,...,字符串n)。
(3)用Join方法是將字符串?dāng)?shù)據(jù)合并為一個(gè)新的字符串,格式為:string.Join(合并后的分隔符,字符串?dāng)?shù)組)。
例三,實(shí)現(xiàn)對字符串str1和str2的合并
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);//將數(shù)組中元素合并為一個(gè)新的字符串 Console.WriteLine(str3); Console.WriteLine(str4); Console.WriteLine(str5); Console.ReadLine(); } } }
輸出的結(jié)果為:abcghj
abcghj
123|456|789
四、字符串的替換
字符串的替換是通過Replace方法實(shí)現(xiàn)的,格式為:字符串.Replace(要替換的原字符串,替換后的字符串);
例四、實(shí)現(xiàn)對字符串str的替換
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(); } } }
輸出的結(jié)果為:|bc|df|slfj
五、字符串的插入與填充
字符串的插入是通過Insert方法實(shí)現(xiàn)的,其格式為:字符串.Insert(插入位置,插入字串)
字符串的填充是通過PadRight方法和PadLeft方法實(shí)現(xiàn)的。
PadRight方法是在字符串的結(jié)尾通過添加指定的重復(fù)字符填充字符串,格式為:字符串.PadRight(總長度)(以空格填充)和字符串.PadRight(總長度,要填充的字符)。
PadLeft方法是在字符串的開頭通過添加指定的重復(fù)字符填充字符串,格式為:字符串.PadLeft(總長度)(以空格填充)和字符串.PadLeft(總長度,要填充的字符)。
例五、實(shí)現(xiàn)對字符串str的插入與填充
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(); } } }
輸出的結(jié)果為:
六,字符串的刪除
字符串的刪除是通過Remove方法實(shí)現(xiàn)的,格式為:
(1)字符串.Remove(開始位置)
(2)字符串.Remove(開始位置,移除數(shù))
其中,開始位置是指字符串的索引,是一個(gè)整數(shù),且小于字符串的長度。第一種格式,是將字符串開始位置后的所有子子符刪除,而第二種是將從開始位置開始數(shù)到移除數(shù)位置的字符刪除。
例六,實(shí)現(xiàn)字符串str的刪除
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開始后數(shù)5個(gè)長度的字符 Console.WriteLine(delstr1); Console.WriteLine(delstr2); Console.ReadLine(); } } }
輸出的結(jié)果為:012345
01234
七、字符串的復(fù)制
字符串的復(fù)制是通過Copy方法和CopyTo方法實(shí)現(xiàn)的。若想把一個(gè)字符串復(fù)制到另一個(gè)字符數(shù)組中,可以使用String的靜態(tài)方法Copy來實(shí)現(xiàn)。其格式為:string.Copy(要復(fù)制的字符串)。
CopyTo方法可以實(shí)現(xiàn)Copy同樣的功能,但是功能更為豐富,可以復(fù)制原字符串的一部分到一個(gè)字符數(shù)組中,其格式為:CopyTo(要復(fù)制的字符起始位置,目標(biāo)字符數(shù)組,目標(biāo)數(shù)組中的開始存放位置,要復(fù)制的字符個(gè)數(shù))。
例七,實(shí)現(xiàn)字符串str的復(fù)制
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(); } } }
輸出的結(jié)果為:This is a string
is a string
八、字符串的大小寫轉(zhuǎn)換
字符串大小寫轉(zhuǎn)換是通過String類的ToLower方法和ToUpper方法實(shí)現(xiàn)的,ToLower方法是將字符串轉(zhuǎn)換為小寫形式,ToUpper是將字符串轉(zhuǎn)換為大寫形式。
例八,實(shí)現(xiàn)字符串str的大小寫轉(zhuǎn)換
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(); } } }
輸出的結(jié)果為:this is a string
THIS IS A STRING
九、字符串的查找
字符串的查找是通過IndexOf方法和LastIndexOf方法實(shí)現(xiàn)的。其格式為:
字符串.IndexOf(要查找的字符或字符串)
字符串.LastIndexOf(要查找的字符或字符串)
其中,IndexOf方法是返回要查找的字符或字符串第一次在所要查找的字符串出現(xiàn)的位置,LastIndexOf方法是返回要查找的字符或字符串最后一次在所要查找的字符串中出現(xiàn)的位置。IndexOf方法和LastIndexOf方法都返回一個(gè)整數(shù),如果在所要查找的字符串內(nèi)不包含要查找的字符或字符串則返回一個(gè)負(fù)數(shù)。
例九,實(shí)現(xiàn)字符串str的查找
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第一次出現(xiàn)的位置是:{0}",rh1); Console.WriteLine("字符i在字符串str最后一次出現(xiàn)的位置是:{0}", rh2); } else { Console.WriteLine("字符i在字符串str未出現(xiàn)"); } Console.ReadLine(); } } }
輸出的結(jié)果為:字符i在字符串str第一次出現(xiàn)的位置是:2
字符i在字符串str最后一次出現(xiàn)的位置是:13
十、字符串的追加
在使用System.String類中的方法時(shí),都要在內(nèi)存中創(chuàng)建一個(gè)新的字符串對象,這就需要為該新對象分配新的空間。在需要對字符串執(zhí)行重復(fù)修改的情況下,與創(chuàng)建新的String對象相關(guān)的系統(tǒng)開銷就可能非常高。為了解決這個(gè)問題,C#提供了一個(gè)類StringBuilder。
使用StringBuilder類時(shí),首先要引入System.Text命名空間,然后通過new關(guān)鍵字對其進(jìn)行初始化。StringBuilder類的方法使用和String類的方法使用是一樣的。
例十、實(shí)現(xiàn)對字符串str的追加
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(); } } }
輸出的結(jié)果為:---Append---
追加后的字符串為:Hellow World! What a beautiful day
補(bǔ)充:轉(zhuǎn)義字符
轉(zhuǎn)義字符具有特定的含義,不同于字符原有的意義的字符。在C#語言中,轉(zhuǎn)義字符是指“\”,主要用來表示那些用一般字符不方便表示的控制代碼。
對于轉(zhuǎn)義字符的輸出C#語言有著特殊的格式:
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(); } } }
輸出的結(jié)果為:
以上就是C#的String類全部學(xué)習(xí)例題,很詳細(xì)的學(xué)習(xí)教程,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
C#中實(shí)現(xiàn)抽象類里建立靜態(tài)方法
這篇文章主要介紹了C#中實(shí)現(xiàn)抽象類里建立靜態(tài)方法,需要的朋友可以參考下2014-07-07C# 從Excel讀取數(shù)據(jù)向SQL server寫入
這篇文章主要介紹了C# 從Excel讀取數(shù)據(jù)向SQL server寫入的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03c#基于NVelocity實(shí)現(xiàn)代碼生成
這篇文章主要介紹了c#基于NVelocity實(shí)現(xiàn)代碼生成的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01

C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法

C#使用動(dòng)態(tài)規(guī)劃解決0-1背包問題實(shí)例分析

DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取公共父節(jié)點(diǎn)的方法

DevExpress之餅狀圖突出(Explode)設(shè)置實(shí)例

關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法