下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
- ?: F1 }9 ?' I `4 W+ W3 Whttp://www.itwis.com/html/net/c/20100506/8234.html
2 P) S! S- c: i( C* d w. Z
~6 @; c& {' g7 F( T7 E A程序代码
! [: g& }2 `: y/ B% R8 C! ?1) public string[] Split(params char[] separator). E0 @% n* F0 ]6 s+ X5 I
2) public string[] Split(char[] separator, int count)
% u. l. Z0 ~* f% @8 G* h3) public string[] Split(char[] separator, StringSplitOptions options)
. \4 R& Q! Q" `) N' h4) public string[] Split(string[] separator, StringSplitOptions options)
8 g' B. ]7 L' X+ W$ x5) public string[] Split(char[] separator, int count, StringSplitOptions options)9 V$ A, N# v& ^- l- X" n" R4 {+ ]
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
9 M7 c( U: R4 t; S+ S9 `. x7 {, v. J- o! e
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):0 O0 ~) y0 m9 B+ ~% F' L p& R2 b1 b
) E: ]3 M% j8 Q# Y2 j/ K8 |
1. public string[] Split(params char[] separator)
. M, u2 d; l* ~% O+ L' g; q( s0 U3 [! d4 Q# }+ r& ]
程序代码
1 ^; s' |' x4 u4 l! p& r2 zstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}! U6 h4 i2 @. B1 t. o! ~
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}+ Q; |3 y' P* M( L$ A: ]0 u$ e
" S' R G: t) W+ @8 G* E
2. public string[] Split(char[] separator, int count)! }/ s2 c! V0 N; j' u9 E9 n" }# i
5 x6 L% R4 e! l u 程序代码$ B# U( A5 H+ O8 e k$ b
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}. U7 u n, F) g, N3 ]6 N
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
- q7 [, s1 R+ I: H$ [# Y/ J$ p, G/ `6 ]8 W3 {' a
3. public string[] Split(char[] separator, StringSplitOptions options)
% p2 t& s. U I+ F# ]
! k+ Q' v+ `+ i2 _6 V 程序代码& a9 w' g: p0 `
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
, u1 R' U# W/ D' n* C% Istring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 K1 r' p" U' w( X- r9 z
) M& o# e% ^1 ~# i4 L& ~+ S
4. public string[] Split(string[] separator, StringSplitOptions options)
* V, y2 B- g1 X! z0 r8 N% _; _) G3 D
程序代码 r) C. B# v) u! d6 t
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
! Z& z/ L, i+ `: ^1 n0 Sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 d \; D# G3 G b0 ~- e) ~7 A9 _& E; W+ E: h0 {5 o: N* J
5. public string[] Split(char[] separator, int count, StringSplitOptions options), f6 J, p( t# f: ]6 G3 n* n% F
2 L }2 J: e. c2 r 程序代码
2 ^7 W* Y5 ~( _- ~string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
" F# G( q( ~7 vstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6 @9 }) X" ~2 E9 h
! h: u- ~ |5 D( G6 T' ^6. public string[] Split(string[] separator, int count, StringSplitOptions options)
: }' f4 y9 A2 d1 E5 O: i0 c9 \4 |3 p
代码
" T" r+ T) f, }; Ustring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素5 X8 K! f; D; U
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |