下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看% K! i1 N4 Q& A" a2 ?; x; p$ ~# H, ^
http://www.itwis.com/html/net/c/20100506/8234.html6 g/ |. u, t! U! N
f; z+ b. T8 h m$ ]程序代码
. m8 i7 S; ~+ j; {0 @1) public string[] Split(params char[] separator)5 w7 v6 ?' b5 k X( r. [
2) public string[] Split(char[] separator, int count)$ z/ h2 a4 c+ P; Z( `
3) public string[] Split(char[] separator, StringSplitOptions options)1 I- t) ]" F3 ^% D1 C2 H1 D
4) public string[] Split(string[] separator, StringSplitOptions options)/ i- e4 b/ g- x. l. K' q* m
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
+ G, g3 N1 x) n2 C! g. q6) public string[] Split(string[] separator, int count, StringSplitOptions options)+ t: ~5 } U, n! C' W, G3 U
3 z7 {8 @: W% L下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
3 j5 M' Q- z# }( x: S. x; ?8 D
R$ i/ K- Y7 `1. public string[] Split(params char[] separator)- A7 n5 D6 A5 `8 C% i/ D6 M' Q1 U
- q: S$ U0 o! ]8 s8 O8 U! R6 o& g
程序代码
7 g/ M% t9 c( o' zstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}5 _7 z* T8 z$ {& \, z6 C- N
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
, G# g# f' C8 H, l+ e* s6 r$ B
" F% n, v7 c5 B, O2. public string[] Split(char[] separator, int count)
# p C( r* E' r6 X- p
5 Q* S# Z; }, |7 z) z3 ~+ v; @0 T: ~ 程序代码, X& c6 H7 H2 y( M" p
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}( ?$ u3 Y$ i( O- x2 g
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}* Y0 S z" C( P0 W& }; b9 M( ]" O
$ \7 a. t0 @+ x6 @2 Y+ m
3. public string[] Split(char[] separator, StringSplitOptions options)$ S- z, h) ?& U
5 _: r ]& S( O: O6 m 程序代码
5 Y" u# b9 ~4 estring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
8 Y* j, E! x1 C5 |; g: p' qstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( _/ a5 |. F& @7 w4 B
6 O6 |5 g0 p* G; k0 f4. public string[] Split(string[] separator, StringSplitOptions options)2 G- C4 ?' H" `2 ]" l; o
4 Y1 \0 [# T c& ?, n( s
程序代码
$ L7 K5 F7 ?' g! x( {string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* Z! D2 m) I6 f+ P& J/ n
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
! V6 w# k/ {6 q" K* x7 f* C) r' J7 ^2 y. I
5. public string[] Split(char[] separator, int count, StringSplitOptions options)2 F* x: b, i& L7 G+ ~
4 D( c6 T; Z& \: I9 c& S0 n+ A 程序代码1 S7 T/ J8 _" h) C8 U* F
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
% N/ \& t# e5 G( c9 K# u! ^% y% _string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 U; A$ o' a. q* Y% }: t" {1 y7 d) \" i. {& C; b' D8 }
6. public string[] Split(string[] separator, int count, StringSplitOptions options): [- R6 \8 w4 o( o$ C4 N
1 C0 j- m3 D( n9 n1 Y, n4 I4 j, ^9 K, t: {
代码2 V, S. m3 a7 M4 L2 |/ E0 h( K
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素- h$ l7 V4 ]: D+ R
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |