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


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?


