下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看 H W( q* r( F# r7 J
http://www.itwis.com/html/net/c/20100506/8234.html
# h3 B8 |/ w& U. U4 O* ^0 b7 ?
' M. m9 _, _$ P7 r0 `+ G% p7 J程序代码5 x; E# ~2 Y* X& G1 m3 P" e; k* J
1) public string[] Split(params char[] separator) Z; H1 O7 K; T5 K) a5 j- t
2) public string[] Split(char[] separator, int count)
* `) l0 N& V7 }: {& \3) public string[] Split(char[] separator, StringSplitOptions options)5 s- W( M' F+ @% \
4) public string[] Split(string[] separator, StringSplitOptions options)
2 N3 s; Q t! J) C5) public string[] Split(char[] separator, int count, StringSplitOptions options)
; B2 I# k9 q0 Y% u0 _/ R" i, f& n7 o6) public string[] Split(string[] separator, int count, StringSplitOptions options)! }: r0 y7 \& N! E# q! I
4 I4 w% j) f. M8 l- } V
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):+ U% I5 j z, q; |3 @+ m! ]$ q$ i
% u0 W! r" C* I- ~, C! L. G, p1. public string[] Split(params char[] separator)# z9 \& `, w1 R; _2 W2 g
( x! X5 @% J0 U8 }/ y! [$ X; n
程序代码
2 D4 v( Z/ J$ ?8 x) O P' k" lstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
+ z4 z4 j* v) L8 mstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
. z. M5 q! C/ C% t, ^4 {9 H3 {4 l5 j: `
2. public string[] Split(char[] separator, int count)/ t- D" S& z, \* G: F/ ~
4 o( Y7 B% E. _: v
程序代码
N$ y; B; _% C. N+ Q5 |string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}7 @+ @- V8 r+ ^4 u# H; l' s5 Y
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}' D; K: _. C r! E( S5 ~
, C3 y* M- u. A- U6 D: Z# S3. public string[] Split(char[] separator, StringSplitOptions options)0 |* H1 [( a) q) s) p5 @1 l
' x1 M7 O1 K2 e6 T 程序代码* |8 E! \! U) `. O! @
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
! ^/ h% ?0 m- istring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素8 c2 E" h$ N; d% k* u& q" I
$ O( ^, q8 i0 @7 A' A/ p3 S+ P4. public string[] Split(string[] separator, StringSplitOptions options)
' Z, S, o( }* q! O6 f7 @/ _2 D% i* W( ?! ~- f" H
程序代码# W$ h, ^! Y: l
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素/ |& W( k( l4 Q1 P2 e; _+ {
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
# H. D( l. E/ ~: x( T+ X: p# y3 H# c$ O, N* ?6 S
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
7 W8 @, R. B" e$ C+ g7 g, F0 h/ C+ S, m
程序代码" N9 e/ t( k6 O% z) T- G# e
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
4 T- F# C. S$ Bstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素( o0 e0 L7 n- A% C, f! M+ h; s, N2 B
4 X4 H; D3 l& l6. public string[] Split(string[] separator, int count, StringSplitOptions options)/ T- v! ], L' P( P1 C8 z
1 e: O/ ~$ u1 \7 O代码
8 Z+ a. Z. Z0 |# j( d( E3 b+ ^string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素3 T4 Z6 J9 b! [. f- H
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |