下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看6 J# q% h3 b% B+ R4 I/ U
http://www.itwis.com/html/net/c/20100506/8234.html9 P$ w) [% V% J6 f1 E' p
3 }( U9 S8 M3 F; j$ v3 W ]+ ?
程序代码 c- o5 {; J. X/ {/ }, G
1) public string[] Split(params char[] separator)! J+ y5 p0 @( `
2) public string[] Split(char[] separator, int count)$ P: t8 @+ Y) }
3) public string[] Split(char[] separator, StringSplitOptions options): S" @* e3 n0 T$ b9 `- u
4) public string[] Split(string[] separator, StringSplitOptions options)
5 b8 @: R) r, V4 r# e+ ?# V6 |5) public string[] Split(char[] separator, int count, StringSplitOptions options)
& L# g" U* [' T7 c) G6) public string[] Split(string[] separator, int count, StringSplitOptions options)
]5 g1 q, _9 L) z6 Y; i& V$ b; {: v5 d* I
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):2 `4 c/ p$ Y8 N9 q# c" g4 \
2 v- Y* \" o* N% i5 l) r1. public string[] Split(params char[] separator)1 E$ l2 U2 }! O8 d: @! e0 v6 t. k
3 c! k( E; }' r' {0 {0 V& p# m
程序代码
3 C1 r7 w2 ]" w$ L( p+ f7 Vstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}- @( o9 s* x+ S+ `% s7 M" s! o
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}$ T2 n5 k! N% C9 A1 n9 e
4 A7 J+ B4 Z; [7 N C$ A
2. public string[] Split(char[] separator, int count)
' P& u9 W& m0 Z% G1 A% @! E# j# n$ W+ q/ e, u+ J8 Q' j
程序代码& r+ j2 H9 V7 _) Z+ q
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"} z+ W7 U3 m8 j2 Q( R
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}, c' E4 t3 r% p6 u; ?4 U
2 p2 z E" p. a ?
3. public string[] Split(char[] separator, StringSplitOptions options)( ^- i# x3 J) Z( M7 L
2 d. A: ?8 k0 w& B
程序代码' J- c, w3 O9 `) _
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
8 Q* V) `; ~' ]: Sstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素" C2 t0 ~0 U( D2 Z
, F: }! X( M& W. t- a1 H: v4. public string[] Split(string[] separator, StringSplitOptions options)1 p; F, i8 a4 D' q |5 n( H/ L
9 f0 Q7 J7 H& ]! k
程序代码% Y# b- _8 K) N* f# a8 c
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# I. o$ A0 |. P
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ h k- c3 G% x# b" Y2 _1 T
& D. U$ r4 ^6 h: Y1 n
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
7 y/ B( d' p8 l: {2 l2 y
+ I- D; f! l; c% \ 程序代码. y u6 I6 _% r0 W( R; {7 L
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素+ z/ l7 B z, C$ Q+ g( T$ a) O: Y/ }
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素5 ~7 f! _2 Z( o! u7 z) z
! D, Y% z9 V5 f; Z) t4 N& W+ d4 D6. public string[] Split(string[] separator, int count, StringSplitOptions options); A: q6 n. _( Z7 j' V2 `% u6 d" y
, E0 T' D7 Z: K0 ^& ~+ l代码
$ ^; D- n' ?& m2 h* ystring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 |4 v- r! S5 o7 o; Zstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |