C#之利用Split分(fēn)割字符串

時間:2020-06-10 作者:管理員(yuán) 點擊:671

字符串是通過Split方法來實現。有多種重載形式。
   格式:字符串1.Split(字符或字符數組),分(fēn)割後返回值是一(yī)個字符串數組。
   string mystr = "str1-str2-str3=str4";
   string[] spstr = mystr.Split('-');//分(fēn)割字符
   foreach(string outstr in spstr)
   {
      Console.WriteLine(outstr);
   }
   string[] spstr=mystr.Split(new char[]{'-','='});//分(fēn)割字符數組
   foreach (string outstr in spstr)
   {
       Console.WriteLine(outstr);
   }
String.Split 方法有6個重載函數:程序代碼
1) public string[] Split(params char[] separator)
2) public string[] Split(char[] separator, int count)
3) public string[] Split(char[] separator, StringSplitOptions options)
4) public string[] Split(string[] separator, StringSplitOptions options)
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6) public string[] Split(string[] separator, int count, StringSplitOptions options)

下(xià)邊我(wǒ)(wǒ)們通過一(yī)些實例來說明下(xià)怎麽使用

(我(wǒ)(wǒ)們定義一(yī)個 字符串 string words = "1,2.3,,4";):

1. public string[] Split(params char[] separator)
程序代碼
string[] split = words.Split(new Char[] { ',' });  //返回:{"1","2.3","","4"}
string[] split = words.Split(new Char[] { ',', '.' });  //返回:{"1","2","3","","4"} 

2. public string[] Split(char[] separator, int count)
程序代碼
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"} 

3. public string[] Split(char[] separator, StringSplitOptions options)
程序代碼
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 
4. public string[] Split(string[] separator, StringSplitOptions options)

程序代碼
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 

5. public string[] Split(char[] separator, int count, StringSplitOptions options)
程序代碼
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 

6. public string[] Split(string[] separator, int count, StringSplitOptions options)
程序代碼
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
返回列表
在線溝通

Are you interested in ?

感興趣嗎(ma)?

有關我(wǒ)(wǒ)們服務的更多信息,請聯系

136 7365 2363(同微信) 13140187702

鄭州網站建設鄭州網站設計鄭州網站制作鄭州建站公司鄭州網站優化--聯系索騰

與我(wǒ)(wǒ)們合作

鄭州網站建設鄭州網站設計鄭州網站制作鄭州建站公司鄭州網站優化--與索騰合作,您将會得到更成熟、專業的網絡建設服務。我(wǒ)(wǒ)們以客戶至上,同時也相互挑戰,力求呈現最好的品牌建設成果。

業務咨詢熱線:

136 7365 2363

TOP

QQ客服

在線留言