下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看/ M' h4 Y$ D `
http://www.itwis.com/html/net/c/20100506/8234.html6 s k1 L4 v# Z `1 [( `" w
& e, ~3 c) f+ ?7 {
程序代码
; y3 P4 p) N6 W3 d1) public string[] Split(params char[] separator)! c$ i2 A! w0 O- O. l% x
2) public string[] Split(char[] separator, int count)! ~/ [- j4 K& M5 z8 w
3) public string[] Split(char[] separator, StringSplitOptions options)5 W. m" i, V( A: u& ]+ y
4) public string[] Split(string[] separator, StringSplitOptions options)! r7 p* g9 ~& }( i: d
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
1 a% \ C; I. H' W: v) N6) public string[] Split(string[] separator, int count, StringSplitOptions options)) T" }8 m! w5 e4 q
& y% R$ C+ t6 A下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
% J( ?1 F0 z1 h0 m2 }6 B- Q7 l7 h. q. I% |% _
1. public string[] Split(params char[] separator)
& s! C& K6 i3 G* u" L& L2 W2 E
程序代码1 W S) ]# ]0 Q$ H/ a3 W8 i
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}5 }5 L4 W4 `5 d4 y; W2 Y0 w
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}) l i) b) I! ^- p/ U
) \- Y1 Z- Q4 i* s! l, d5 D. _0 \ @2. public string[] Split(char[] separator, int count)
- n5 x6 q/ ?! M% V3 r
9 J& ]# `- z. z 程序代码
2 i7 i" }" ]5 g vstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}' ^9 k7 ?/ U: Z0 M
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
7 O4 e) V. s. J! j8 J4 Q
. i% Z: ]2 g; y* v3. public string[] Split(char[] separator, StringSplitOptions options)
! j! K: [8 U& a) X; F t8 |
2 `/ a9 m9 e: R2 Z4 w2 S 程序代码
$ e$ K7 w, u' O( E* o3 ~" Ostring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素- s$ _8 l8 [; z' a {! f1 S- e
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
9 _+ o* F; z- y1 u/ d
0 W' T& D& q6 x7 p/ w9 @' ^4. public string[] Split(string[] separator, StringSplitOptions options)
% v% O2 m6 ^4 O! E! o
- m# u9 V/ _1 i! o A7 X( A 程序代码
* `0 e2 w6 J* k) \1 T1 G5 mstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素2 ?" B+ Y' r$ s- O( Y* r
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
c2 g$ l" A- ^, P; @% G' |. @4 D5 G( E: `# z8 F* m
5. public string[] Split(char[] separator, int count, StringSplitOptions options)* _! C- i5 L% Q0 ]' }9 p6 v
2 o1 A8 S* C Y( B- c
程序代码4 |. H$ q6 g. U! f
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
3 d8 e) R: e$ T6 h% h5 t6 Q) w* f7 bstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
/ ]7 y4 v' t0 m
% Q+ ^, U+ K% ?6 r& J% n3 @, \6. public string[] Split(string[] separator, int count, StringSplitOptions options)
" p- Z# r" p1 K. Z
O9 g5 |0 A* \6 Y% V) L6 f代码0 ?; _ m2 h) [7 x
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素3 B7 X* `5 G# M' O- m
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |