下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看) j! D: e. t$ M" a9 d" g
http://www.itwis.com/html/net/c/20100506/8234.html: A( J7 Y6 R7 O- g9 d p
( P) s9 B; |9 `4 g/ S1 f) t程序代码 x6 E5 ]7 K& U2 n% O
1) public string[] Split(params char[] separator)
7 s0 ?" y: i- n' N: s, p3 P$ @2) public string[] Split(char[] separator, int count)* N; o1 P7 |% _% w7 E: B$ A
3) public string[] Split(char[] separator, StringSplitOptions options)
( A! Z9 r) S& i2 n9 `+ A4) public string[] Split(string[] separator, StringSplitOptions options)
) ]! A4 H5 {$ f$ Q& h f5 Z5 z) i6 D5) public string[] Split(char[] separator, int count, StringSplitOptions options)* }& U" j- X8 w3 {
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
; C8 ?. k/ G8 w' ~ R, {, o% C* f8 h* F' p0 X/ W4 R. [9 e: Y7 p
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
9 t" W( Y; a6 M$ w4 C9 k U+ E- P& u3 \9 Z
1. public string[] Split(params char[] separator): j8 ]) ]" I( o
) ?2 e+ d( }" R7 d3 T 程序代码
( W- h, x+ l/ Qstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
& D6 X K7 V0 X K- t1 I$ Cstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}) o% O2 \# P0 u3 N# w
; b( `' J3 ?/ O$ P: N2. public string[] Split(char[] separator, int count)
* U, t# o- z2 p- W; \, c- Z7 K* n/ a% s$ ^
程序代码
2 l- t5 M; C1 H$ B: N) d, U4 X% Pstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}! k# i$ \6 m" w8 _! x7 p
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
4 `- x5 p" ?( t2 ]% G0 l1 O1 j( s$ s& p
3. public string[] Split(char[] separator, StringSplitOptions options)
. J4 b1 y( ]: V$ R9 h, F# d/ K F2 f" T
程序代码
7 Z; \! V& B6 \$ `' y% r4 n" cstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
2 }% s8 `) y2 Jstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
8 C8 V6 }$ ^, X- `8 X9 \5 {1 | t$ }; W: z" A: ?; f0 D
4. public string[] Split(string[] separator, StringSplitOptions options)% h+ A; y* j; O8 H2 d
# X4 x9 l, ^. S, {2 p& [$ e
程序代码9 G6 C2 Q# B$ e- `9 s
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
t8 |8 A$ P7 `5 K2 s3 ?4 sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素* f3 n7 G, B3 r8 R- Q
0 v3 ?! `0 k: b, E" v5. public string[] Split(char[] separator, int count, StringSplitOptions options)6 I" l+ s4 `: i1 ^" A2 }8 Q$ L
, p8 o- B' ]* N* B* O: e 程序代码9 Z8 G# g% U2 p( [8 Z$ O7 Y+ g
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素. j, |" v* T- o1 v
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
9 c: ]3 `. v, G4 K7 M# L# G% ?9 z4 T
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
" G/ h( b+ f+ c4 _+ Z5 {
" j' u7 P) H3 G9 ]代码3 |0 E, e) W0 a. }( N/ H P
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
! O" P& G5 |- r( F2 ]string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |