下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
; S( W4 D7 n1 lhttp://www.itwis.com/html/net/c/20100506/8234.html
+ D+ y# g, G& ~2 m- z' _6 [4 O& |6 ]6 ?( {, x' @
程序代码
6 k, `2 w$ l. G: ]4 W- U8 h9 _1) public string[] Split(params char[] separator)
/ Z) W. w- ^ u7 n4 B- G2) public string[] Split(char[] separator, int count)' d6 s* Q9 ?3 F$ v. N
3) public string[] Split(char[] separator, StringSplitOptions options)/ U; ]0 |4 S+ q' W
4) public string[] Split(string[] separator, StringSplitOptions options)8 f& C4 Q: G1 T9 [6 H' ^
5) public string[] Split(char[] separator, int count, StringSplitOptions options)' E5 q0 X+ K' |8 Y
6) public string[] Split(string[] separator, int count, StringSplitOptions options)" M# r; B- _1 c+ \ w" y
! P) E8 {2 d Y S1 S* b. f/ K
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):; w( M- q; U/ a2 Z: i3 k9 z
: \. [. U" f3 j8 W8 w
1. public string[] Split(params char[] separator)
+ }. B5 s! F2 ~' G) z6 b6 m! b& \* h& P6 x7 s+ y
程序代码 ]4 u8 ~, n& A# c) [
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 M& H$ P6 \* y$ u3 Y
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}: h, U1 C2 G" A6 o: y1 f& I
, e* }9 S' i6 V: v/ @. e2. public string[] Split(char[] separator, int count)
& x. n3 v6 s' R, J2 R9 l. J3 l; W4 K2 w$ t* h0 N
程序代码
) _9 P }3 u4 x0 ^string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}+ j. V' L" K: L6 o
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
$ b/ [4 d% u: j+ z; U I! o/ V( v) l& U
3. public string[] Split(char[] separator, StringSplitOptions options)
5 ~& ]6 G f$ o8 r' g7 x% `4 u0 j1 j) `( l' H) A8 m
程序代码5 a1 e) W3 u5 _7 u! P3 d
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
9 {% q7 u5 w5 Q8 Qstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素' @$ R3 A5 N+ _: T
3 \$ X* Y9 m$ c- _4. public string[] Split(string[] separator, StringSplitOptions options)6 D1 s! H0 z4 d2 S1 v
5 ]+ Q0 y) Z3 }" ^( h+ Y, s6 i
程序代码6 {" d% n3 S- v: Z
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素7 a; Q0 f4 o% F2 X- w
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素3 y6 K% d! ~2 h8 E
) r2 B1 t5 B& l @9 r7 s4 a5 y
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
; m9 I" H0 \3 S: U
% {( G: U r# K2 g) f/ y 程序代码
2 P" I7 a' \: p6 R" [string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
: D5 Z1 {$ n @string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 u8 L6 R7 S: l7 y/ l5 C
& J3 W2 |, W0 A0 n6. public string[] Split(string[] separator, int count, StringSplitOptions options), J$ M! A x0 A* ^
7 h! g' v5 p2 ?: {
代码' f4 f# G3 U- E/ }0 m# F3 B, m. A
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素, _, L/ Z/ S" Z' H( I X
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |