下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
9 L8 P+ o+ ^- v: Hhttp://www.itwis.com/html/net/c/20100506/8234.html+ B1 M6 j& g( f
8 c2 [, b; f1 [
程序代码0 C. o$ \2 c; e
1) public string[] Split(params char[] separator)
0 ~/ d9 s% ~8 y( d H7 i; U1 h2) public string[] Split(char[] separator, int count) p4 u/ \% ]: g" ^4 n- r! c
3) public string[] Split(char[] separator, StringSplitOptions options)
* Q0 z2 y3 U8 w2 U7 {4) public string[] Split(string[] separator, StringSplitOptions options)
1 r) Q3 T+ x( N0 j; o8 h6 }5) public string[] Split(char[] separator, int count, StringSplitOptions options)7 d* s% F- I1 L$ f. W# @/ u
6) public string[] Split(string[] separator, int count, StringSplitOptions options)3 _' s C+ N5 |7 v$ Q
: O' Y- A+ W) }) D( P下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):5 |) D! s; F* g( P) w& W2 z
% r: _* ?# c h0 p3 @/ C, @- T. w& k1. public string[] Split(params char[] separator)
( C# m% }( T( W- z {" W1 i( j
' Q# ~6 L4 s- ^9 @( V% ?$ p+ `* t 程序代码
T/ z7 x+ J. z' g t+ kstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
& X$ k* L9 J8 l2 K M+ lstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}5 |0 ]8 [, t. e* t1 v
3 r- T) d. r x: ^- _$ x) M
2. public string[] Split(char[] separator, int count)
6 o& H- f. v/ s4 K2 x9 R9 E, n" F/ R. ]. E: d
程序代码; X7 F1 k1 S" N: h/ h
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
7 j5 V4 v$ @* e6 ^0 B# d5 P, Ostring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
8 o( v3 A* |* V5 ]
9 {9 f/ A$ Q( W$ u) N; z2 o3 `3. public string[] Split(char[] separator, StringSplitOptions options)( Z" L7 s- A5 `% N
' A; H6 e' y0 ], ?/ ?- K( X 程序代码) _5 T/ }7 c2 r! K. S
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
" |: n/ q5 }) nstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素0 e# n) I; ~: O* X7 ?& ~
4 O7 s2 b* u4 ] }: E* C( S, n9 |4. public string[] Split(string[] separator, StringSplitOptions options)
2 c! p3 x; d. Y3 E
8 T! {' F: O$ ~, m 程序代码
( h; Q' s! T% jstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
! l/ _+ K* c# e9 Wstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: ]6 r" ^! Z1 ` y3 l0 N- C/ }+ P/ w) T! C1 |; z3 v
5. public string[] Split(char[] separator, int count, StringSplitOptions options)( q0 l" F3 x$ x, `9 Y
0 v; x) c- e& a' y4 S1 I; ]
程序代码1 k2 q) i/ _" W5 J9 x
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素0 A s# m' {) C
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素3 S1 ]2 S' G8 S
+ ?0 y) }4 x" T
6. public string[] Split(string[] separator, int count, StringSplitOptions options)2 Q8 ]2 E5 g8 D/ D0 ~! {
) p4 U( j3 u0 M, ?$ {$ s3 b+ U代码* I7 q! c7 h O* E* t4 h# y9 e2 g
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素7 P: V6 Y' X5 \4 F3 @$ _0 E& s6 x
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |