C 中Split用法 字串分隔

2021-09-07 14:23:42 字數 3103 閱讀 3909

using system.text.regularexpressions;

string str="aaajsbbbjsccc";

string sarray=regex.split(str,"js",regexoptions.ignorecase);

foreach (string i in sarray) response.write(i.tostring() + "

");

輸出結果:

aaabbb

ccc2、用多個字元來分隔:

string str="aaajbbbscccjdddseee"; 

string sarray=str.split(new char[2] ); 

foreach(string i in sarray) response.write(i.tostring() + "

"); 

輸出結果:

aaabbb

cccddd

eee3、用單個字元來分隔:

string str="aaajbbbjccc";

string sarray=str.split('j');

foreach(string i in sarray) response.write(i.tostring() + "

");

輸出結果:

aaabbb

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

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

string str = "technology";

char separator = ;

string arr = str.split(separator);

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)

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

1. public string split(params char separator)

程式**

string split = words.split(new char );//返回:

string split = words.split(new char );//返回: 

2. public string split(char separator, int count)

程式**

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, int count, 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, int count, stringsplitoptions options)

程式**

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

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

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

C 中Split分隔字串的應用

因工作需要,總結了幾種分隔字串的方法 1 用字串分隔 using system.text.regularexpressions string str aaajsbbbjsccc string sarray regex.split str,js regexoptions.ignorecase fore...

C語言中的字串中的分隔 split

這個方法中運用到了strtok函式 原型 char strtok char s,const char delim 功能 分解字串為一組字串。s為要分解的字串,delim為 分隔符字串。例如 strtok abc,def,ghi 最後可以分割成為abc def ghi.尤其在點分十進位制的ip中提取應...

C 中Split用法 字元 陣列串

1 用字串分隔 using system.text.regularexpressions string str aaajsbbbjsccc string sarray regex.split str,js regexoptions.ignorecase foreach string i in sar...