C 中split的用法

2021-07-10 10:36:36 字數 3497 閱讀 8821

**:

1、用字串分隔,使用到了regex類的split靜態方法

[csharp]view plain

copy

using

system;  

using

system.collections.generic;  

using

system.linq;  

using

system.text;  

using

system.text.regularexpressions;  

namespace

csharptest  

}  }  

輸出結果為:

aaabbb

ccc2.用多個字元來分隔:(只有遇到字元陣列中的乙個字元,就會作為分隔符)

[csharp]view plain

copy

using

system;  

using

system.collections.generic;  

using

system.linq;  

using

system.text;  

namespace

csharptest  

);  

foreach

(string

i in

sarray) console.write(i.tostring() + 

"\n"

);  

console.readkey();  

}  }  

}  

輸出結果:

aaabbb

cccddd

eee3.用單個字元來分隔:

[csharp]view plain

copy

using

system;  

using

system.collections.generic;  

using

system.linq;  

using

system.text;  

namespace

csharptest  

}  }  

輸出結果:

aaabbb

cccstring.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)itpub個人空間,n:h!c0m/s3u\u0002p

程式**

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);//返回: 不保留空元素

\u0002w1i+ch%^\u0017}0string 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);//返回: 不保留空元素itpub個人空間1k;e\u0007f\u0008f }\u0011c n

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的用法

第一種方法 string s abcdeabcdeabcde string sarray s.split c foreach string i in sarray console.writeline i.tostring 輸出下面的結果 abdeab deab de第二種方法 我們看到了結果是以乙個...

C 中split的用法

第一種方法 string s abcdeabcdeabcde string sarray s.split c foreach string i in sarray console.writeline i.tostring 輸出下面的結果 abdeab deab de第二種方法 我們看到了結果是以乙個...

C 中split的用法

第一種方法 string s abcdeabcdeabcde string sarray s.split c foreach string i in sarray console.writeline i.tostring 輸出下面的結果 abdeab deab de第二種方法 我們看到了結果是以乙個...