c 中Split 分離字元以及空格消除方法

2022-06-09 19:36:12 字數 3732 閱讀 8153

1        split幾種分離方法

1)用字串分隔: using system.text.regularexpressions; string str="aaajsbbbjsccc"; string sarray=regex.split(str,"js",regexoptions.ignorecase); foreach (string i in sarray) response.write(i.tostring() + "

"); 輸出結果: aaa bbb ccc 2)用多個字元來分隔: string str="aaajbbbscccjdddseee";

string sarray=str.split(new char[2] ); foreach(string i in sarray) response.write(i.tostring() + "

"); 輸出結果: aaa bbb ccc ddd eee 3)用單個字元來分隔: string str="aaajbbbjccc"; string sarray=str.split('j'); foreach(string i in sarray) response.write(i.tostring()+"

"); 輸出結果: aaa bbb ccc

string arr = str.split("o");

這是乙個具有語法錯誤的語句,split 的 separator 引數應該是char或 string,不應是字串。正確的示例:

string str = "technology"; char separator = ; string arr = str.split(separator);

4)用system.text.regularexpressions(框架中的正規表示式來根據字串擷取) string str3="hellomrzhanghellomisslihellomrzhao";  string strname=system.text.regularexpressions.regex.split(str3,"hello",system.text.regularexpressions.regexoptions.ignorecase); foreach(string var in strname) 

//結果 //mrzhang //missli //mrzhao

regex.split()中的第2個引數可以是正規表示式

string.split 方法有6個過載函式:

程式**

1) public string split(params charseparator)

2) public string split(char separator, intcount) 3) public string split(char separator, stringsplitoptions options) 4) public string split(string separator, stringsplitoptions options) 5) public string split(char separator, int count, stringsplitoptionsoptions) 6) public string split(string separator, int count, stringsplitoptionsoptions)

2 例項解析

下邊我們通過一些例項來說明下怎麼使用(以下string words = "1,2.3,,4";): 1) public string split(params charseparator)

程式**

string split = words.split(new char );//返回://用逗號分開 string split = words.split(new char );//返回://用逗號和點分開 2) public string split(char separator, intcount)

程式**

string split = words.split(new char , 2);//返回: string split = words.split(new char , 6);//返回: 3) public string split(char separator,stringsplitoptions options)

程式**

string split = words.split(new char , stringsplitoptions.removeemptyentries);//返回:不保留空元素 string split = words.split(new char ,stringsplitoptions.none);//返回: 保留空元素  4) public string split(string separator,stringsplitoptions options)

程式**

string split = words.split(new string , stringsplitoptions.removeemptyentries);//返回:不保留空元素 string split = words.split(new string ,stringsplitoptions.none);//返回: 保留空元素  5) public string split(char separator, intcount, stringsplitoptions options)

程式**

string split = words.split(new char , 2, stringsplitoptions.removeemptyentries);//返回: 不保留空元素 string split = words.split(new char , 6,stringsplitoptions.none);//返回: 保留空元素  6)public string split(string separator, intcount, stringsplitoptions options)

程式**

string split = words.split(new string , 2, stringsplitoptions.removeemptyentries);//返回: 不保留空元素 string split = words.split(new string , 6,stringsplitoptions.none);//返回: 保留空元素

注意:沒有過載函式public stringsplit(string separator),所以我們不能像vb.net那樣使用words.split(","),而只能使用words.split(',')

3 消除文字中字串中的空格

有時我們會使用string lines=system.io.file.readalllines(@"c:/root.ini", encoding.default);來得到文字中的資訊,但是由於文字檔案可能不規範,所以取到的字串陣列內容有空格,但是有時字串有比較或者輸出的需要,所以這些空格需要處理掉。

1)  字串陣列中有空格項

如會取到 a[0]="aa";  a[1]="   ";  a[2]="321";

處理方案:在取字串陣列的時候,使用removeemptyentries把結果空白字串處理掉

用法例:string split(charseparator, stringsplitoptions.removeemptyentries)

2)取到的字串兩頭有空格

如取到a[0]="    aaa    ";   a[1]="    ad";   a[2]="ddd     "; 

處理方案:這樣我們可以使用trim方法來解決這個問題,遍歷整個陣列,或者指定元素使用

用法例:strings2 = a[0].trim;

msdn----trim(

參考:

c 中Split 分離字元以及空格消除方法

c 中split 分離字元以及空格消除方法 1 split幾種分離方法 1 用字串分隔 using system.text.regularexpressions string str aaajsbbbjsccc string sarray regex.split str,js regexoption...

C 中的Split用法以及詳解

一 string.split方法提供了如下6個過載函式 名稱說明 string.split char 返回包含此例項中的子字串 由指定 char 陣列的元素分隔 的 string 陣列。由 net compact framework 支援。string.split char,int32 返回包含此例...

delphi 的分離字串函式 split

unit unit1 inte ce uses windows,messages,sysutils,variants,classes,graphics,controls,forms,dialogs,stdctrls type userarray array of string type tform1...