下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看# o5 C3 |. z; R# A
http://www.itwis.com/html/net/c/20100506/8234.html
6 @! ` g3 J+ q8 ?7 H- Y+ O3 r9 \. w) Q/ e+ O: S
程序代码# j6 ^5 b" O1 m# i
1) public string[] Split(params char[] separator)8 h; G' G2 y/ k( }
2) public string[] Split(char[] separator, int count)7 ~/ u; m# Y" ~6 _# f
3) public string[] Split(char[] separator, StringSplitOptions options)
4 |2 V! F9 L2 B/ R# G5 r4) public string[] Split(string[] separator, StringSplitOptions options)9 N. x5 N' G/ z# U1 s7 s
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
+ c" ~9 y4 n. a5 I- T6) public string[] Split(string[] separator, int count, StringSplitOptions options)
2 K; f# N$ A1 Y! N& f$ M4 H
1 n0 }9 z. d$ ]3 l, I下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
$ n" S4 b* X( q: _: P5 Z
9 G/ V9 a6 i8 ?" ^ t) p1. public string[] Split(params char[] separator)
$ Z; w5 Z, A9 ^! N/ e' [7 Q* ?+ S6 |+ y8 ~# ]8 r, @
程序代码
. i2 J3 k' N" B: pstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
- \8 a9 q5 }. j) H$ Sstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
4 F- e) l) x; {/ P. ^
+ n9 j& A& i O8 T2. public string[] Split(char[] separator, int count)
- ^% u4 x4 m3 E/ o0 ~9 j( q/ D/ h! j1 D' V5 Q6 P" Q. A
程序代码/ k' H5 j8 Z; q, z
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
3 g+ B I+ ^6 n9 B/ Cstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}7 Y( i# m$ U$ U8 R( H
( U8 {: s3 q2 }; b
3. public string[] Split(char[] separator, StringSplitOptions options)
( f) y% M" Q( Y8 B4 Y3 P$ T O4 E. a# W1 a" Z c) ]) h( N, ^
程序代码0 k, c+ ^. M9 d0 X
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素& K$ M8 O* b7 M- ^2 J5 S; w
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% c$ v/ s, z; |
# }, J# d; z2 w8 L4. public string[] Split(string[] separator, StringSplitOptions options)3 m6 D: d/ F; ?# a ~8 A& F" p
7 f V9 L S7 z3 _- z9 w 程序代码# E/ A8 e9 Q/ M
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素0 t! |3 M! P8 ]
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! d" s5 G2 o% M4 D
& X0 \/ m0 |- q! E
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
/ I. c+ C$ D% r9 {: t6 v8 P1 I* j% v' n. J2 m6 R( M8 p* n% |* ~5 U
程序代码
" g8 b$ E1 k% Sstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# }1 v* m. \: \8 S6 astring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素" u0 t) O) k' z. r( t+ G
9 ]1 J2 A {" u0 l6. public string[] Split(string[] separator, int count, StringSplitOptions options)$ G- ^+ u$ v+ i0 q# O9 z
+ {" D3 e& t9 z h' n
代码; g4 M+ \3 X% y! [. s; L
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素7 J( n5 l' t5 ~' I: Z
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |