下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看) ]1 w0 c2 A+ o4 G4 D
http://www.itwis.com/html/net/c/20100506/8234.html% Z" z. f& Y/ a4 Y
7 D9 Q% z$ i5 [ x! G程序代码
& y3 m- H6 C4 p" N1) public string[] Split(params char[] separator)
9 c* u0 h5 `7 J7 @# B2) public string[] Split(char[] separator, int count)
& A% ]! j+ G8 A9 M3) public string[] Split(char[] separator, StringSplitOptions options)1 z. R: L- s/ ~- @% q: z1 F
4) public string[] Split(string[] separator, StringSplitOptions options)5 l5 ?1 g( d8 D
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
* x- G! V6 Y1 t& y1 f6) public string[] Split(string[] separator, int count, StringSplitOptions options)
0 X( _7 O! b" C" s3 g3 V) \$ y$ d3 h R2 X. x: M* l" x
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):; \3 j0 \ V. b4 L
3 q5 c6 o* @6 R- R1. public string[] Split(params char[] separator)4 @3 I9 _: k. d: ?( g$ U
/ G$ d9 f1 ~1 z# w/ ~
程序代码
, h# }$ \; Y2 _6 E# f. G; N( e# }string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
6 }& I% ^: G; Sstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
, B& G% H7 x( z" j6 Y; h) \' J( G4 O& ?- I& K
2. public string[] Split(char[] separator, int count)/ y% T$ [& m5 I, j) f3 X
' m( ^/ `% n6 c1 W! j/ {' |0 J
程序代码
# B4 c: X" x8 t$ Ustring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}: o/ r/ x; f: e
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
* F2 H7 H9 {0 H3 Y0 p q
3 X3 }2 v3 |: }" n3. public string[] Split(char[] separator, StringSplitOptions options)
; l4 d7 ~2 F3 t( b ?+ L/ L. s3 T
程序代码# v3 o2 C8 V+ M F, z/ O/ l
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
; c, S) c/ o0 L( b7 s1 Cstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素& N1 K, _$ M* m. Y; p3 ?
2 @: S! I8 c% q5 |1 p# {' f; K4. public string[] Split(string[] separator, StringSplitOptions options)
& q+ A) N9 p( h! h
* a) M9 X q5 d |' Y0 Q8 u 程序代码
5 f/ m+ G& x+ y* astring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素3 r0 J. `8 n" c! i& E9 ~
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素: D$ r; u& N1 l+ T4 }) h4 K
( I* o: W! _ P( d+ T# F: T3 ]
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
4 \! a9 l# U& P5 i/ ^& S5 y4 X/ B) {0 q( T& U5 t/ ~
程序代码
$ l# f) g2 b9 Z. H. o" qstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
% i! [- l* n+ O6 Pstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( \9 c. X3 ^+ [- r: E$ `9 K
/ Z9 W7 X6 g$ }! v- K6. public string[] Split(string[] separator, int count, StringSplitOptions options)
3 w( f! H: ?( T" E A+ Q8 U f0 z- v+ g
, T+ l. Z+ z/ J1 H3 `' _代码3 ~7 i- L% ?# O" R2 u* {8 W3 Z2 T6 I
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素& b7 |; `/ W2 y: P4 O' A2 A; u7 i
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |