下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看8 d/ Q0 f' m. a8 F
http://www.itwis.com/html/net/c/20100506/8234.html1 y3 V' c: T$ t4 C! s \
6 B9 ?/ N+ n# F2 Y/ X* [9 i' G程序代码' S! G7 h' i; q. x
1) public string[] Split(params char[] separator). J: J5 b3 A f
2) public string[] Split(char[] separator, int count)) _1 f, @ e+ F. B. h: ?
3) public string[] Split(char[] separator, StringSplitOptions options)
( {2 o" u6 V6 P! N# A4) public string[] Split(string[] separator, StringSplitOptions options)
1 E# f1 N( v& I5) public string[] Split(char[] separator, int count, StringSplitOptions options)
2 ~( y2 M3 M$ E; w6) public string[] Split(string[] separator, int count, StringSplitOptions options)' w, W) I$ a8 h0 Z! {, p
" R2 H9 i0 _1 {. L
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
& _$ r8 ]1 g5 U: @" n( Z0 S7 h) B
) o, P7 j% d7 b/ t8 T1 p/ Y1. public string[] Split(params char[] separator)
! i! N5 z9 a3 q: t
+ R( B s9 Z2 P0 z, V* \4 W 程序代码
) \& T" ?) ? ]6 g# a, f2 w% bstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}. U! j7 r/ n* k
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}# b1 a+ |5 J* t5 p
7 ?# ^8 m6 b$ c% }7 a8 k( Q: ?" R0 z2. public string[] Split(char[] separator, int count)
( ]: _0 M: O6 L9 z( b Y3 Q" v9 H8 }$ e2 |* |( E
程序代码1 |. ^7 p* F) P( p+ q- t
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}6 u+ g! h( e* D! {$ R2 L# d# U
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
& i$ J2 |" A. q
- i$ i3 A/ _2 B3. public string[] Split(char[] separator, StringSplitOptions options)
0 L @' `* V: t' T$ } p
) d% n, E0 W- \+ q7 U6 G, H 程序代码
, J; g+ u Z9 }% J2 Q( f: pstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
/ n2 L- ]5 j& u% g0 Q! fstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素. V: P; I' P1 c0 Y T, Y( G" A
+ z/ f( y/ b* K: q
4. public string[] Split(string[] separator, StringSplitOptions options)
0 |; N6 f x! A2 X$ v5 N* v! b+ @3 u' \& `# D
程序代码
2 c W6 |5 A" z( c, }string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
H# c+ a, p4 h1 [) C1 Lstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素0 Y* k! n/ R9 {3 w* r
6 v2 \! u; G0 F/ b X4 v
5. public string[] Split(char[] separator, int count, StringSplitOptions options)- C0 g0 w. [. N4 s. U
; F: F9 _7 b. c2 G$ x 程序代码
$ M! J& P$ k6 V) @3 M2 S* nstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素& e2 I# L6 d ]' Q; M( d, C% A3 g
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! j% e( i2 a7 ^5 [& _" e
/ [+ S, \# x4 Y) v2 r6. public string[] Split(string[] separator, int count, StringSplitOptions options)
/ P9 F o0 o' ]; W# J% p3 u. w% e l2 ^9 j6 z
代码8 ?" _" Z$ Z, D, q$ Y7 C
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素2 }; B7 e; j+ i
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |