下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看5 B4 M. G9 X" I; P) a% I
http://www.itwis.com/html/net/c/20100506/8234.html
; M; C. K2 ~8 o% x
; P+ t9 w! j: e+ e程序代码
/ |# K! U. M3 J( E) b1) public string[] Split(params char[] separator) y& k; ~9 W1 e) t. C
2) public string[] Split(char[] separator, int count)! }3 u3 g; U. d$ ]. g8 r
3) public string[] Split(char[] separator, StringSplitOptions options): t; Z7 b" c. B3 r; ?- a, ]6 ~
4) public string[] Split(string[] separator, StringSplitOptions options)% p3 N3 s9 \6 k' w( D5 g/ H5 ?
5) public string[] Split(char[] separator, int count, StringSplitOptions options)9 y g% C! \0 w. S9 v0 e6 T/ j
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
; F: }8 m- f, A" V0 ]6 b, I) j2 d5 {8 j, `8 ^) A, E. x
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):, Y5 I; ]- J7 |. w+ Z. N' s. B$ j
0 a5 Z9 K5 W/ I( c, \
1. public string[] Split(params char[] separator)+ r% y' M. l3 c( p; ]2 O
; |, t% Y' r; }
程序代码
& h% J9 n6 X+ D9 d+ j4 m8 @string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}" w4 s) X0 A" t0 s6 T U0 W7 z
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
6 _# O: w2 {0 \4 c; }* v0 c2 ~5 t# a! U. `/ X
2. public string[] Split(char[] separator, int count)" t% U: H( S( P% Q. w7 [" k9 g
) f" g& W: d! ?
程序代码0 `4 [) L* u U; s3 ^, O, Z
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}) L% k7 X3 I, E" y
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}3 A$ {4 J* K( w
3 l3 r6 M `9 L0 h( U1 @% t8 u
3. public string[] Split(char[] separator, StringSplitOptions options)( X: u4 g# f( ^. w; g2 ^
% F: i V# Q) ^: Z 程序代码( Q. L- c, A; U% H1 i% [: h
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
5 ^. }2 `" }( e8 ^9 pstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素: N0 O6 |. R1 @7 g
$ ^; M' W* a- H* a! Z4 H
4. public string[] Split(string[] separator, StringSplitOptions options)6 {. a8 ~, z( L6 o9 S
( O0 d7 s+ \7 P: L2 q/ ^
程序代码6 {# Y8 W$ I% \- f p( V
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
/ F# t/ _+ E( Y; Jstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素4 \, D; M2 c8 c0 P; B: K
1 z. I& ~( r: r; J, D) k' B9 r5 }
5. public string[] Split(char[] separator, int count, StringSplitOptions options); {+ o9 i3 V3 O) J1 p
1 h+ d! x: V) B" e' T: r 程序代码
: s5 \& x9 v9 z2 I: l) k6 Astring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
4 b7 U/ K0 N/ S" I1 }string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' T, q5 ~0 f) [% k8 E, Q; v+ C; ~$ C: }8 N* u) R6 |4 O
6. public string[] Split(string[] separator, int count, StringSplitOptions options)( d9 }9 U: D- i5 K+ S1 T
. O) u- W, f, w v/ ]: C: z. V. Q/ x! n代码! j z0 B& F' A/ V( o1 j# g* c, \/ q
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素6 ? c: D! Q, T# x6 o8 p; S6 {
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |