下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看8 C3 b# b, ^$ s* k
http://www.itwis.com/html/net/c/20100506/8234.html
# p! q6 G* J( @* w% k+ b& f6 v5 `8 M8 X' ]) a" M' Y( p1 m: [
程序代码
* G& y. v/ K0 R( V* Q6 M4 z: j# h& S1) public string[] Split(params char[] separator)
! C* z) `1 ~) {$ g# B/ O2) public string[] Split(char[] separator, int count)! N* ]+ G7 ?* r) K
3) public string[] Split(char[] separator, StringSplitOptions options)' L" b6 b6 y& T/ o0 w! ]/ m
4) public string[] Split(string[] separator, StringSplitOptions options)
. E0 R, j0 P1 q+ n5) public string[] Split(char[] separator, int count, StringSplitOptions options)
H" \ z/ T# k6 s; e: c( X* Q6) public string[] Split(string[] separator, int count, StringSplitOptions options)
" I/ P! s( T& s; M, \+ b0 c8 s; g& O; ^: V7 i5 d! S
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):, J2 E+ ]5 i( v: ^ }6 M7 [
" h8 l4 g1 b% G4 Z
1. public string[] Split(params char[] separator): j, h3 D7 Y! p2 q* W( [
7 [5 w l3 X' e0 C) z" o
程序代码
% m# x% U9 x: _0 l& v7 estring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}' m" F! _9 W" r
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
& n% G+ U7 Q/ Y+ o+ ]6 M, c& z
7 x) F6 d( H2 G) c1 `/ y' h4 C2. public string[] Split(char[] separator, int count)6 I, x' i. F" c7 K& h6 z, k. {
" w1 q; V( x4 n) N
程序代码
# J2 ]+ Q6 a) D7 j$ G/ t+ dstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}- I v, O4 @' T
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
( ^' E5 ?- G( g" y# O: `
; T/ N. D& E% F8 v5 A* F3. public string[] Split(char[] separator, StringSplitOptions options)2 z& H. Y- X9 E% I
% R ?$ ~! h4 `$ V: e4 p 程序代码8 x# T7 p+ y) } W4 ^* o1 X
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
" g* v8 k9 d9 u5 A& l6 T+ [string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' ^& Q+ B; Q" [$ m; }. ^% q1 ~
' S) v% D7 Z+ V" `! I4. public string[] Split(string[] separator, StringSplitOptions options)3 R/ U/ P6 w+ M
7 ~" J a6 L5 p( \1 Y5 y
程序代码# I- p% B6 c+ |, S% F) p( l
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* \: ]9 q* x: X/ r: m1 @# Y
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素' E) w* f/ h! R2 B) S& n1 @* p
& @1 t; G8 C3 F) N* m" F! A5. public string[] Split(char[] separator, int count, StringSplitOptions options)2 ?" z! |5 v1 L; t. W
- G% R2 i. Z1 n2 d 程序代码5 E6 l+ q6 a5 i
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
2 p. r# |/ N! p3 q6 U Y* Astring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素* u, l0 H" D' H! p3 o
- ?) t' B9 w' O7 K1 q6. public string[] Split(string[] separator, int count, StringSplitOptions options)3 v$ \8 P, ^( B# ^6 k* C7 Y- B& S; r8 ]
4 M* g) f+ H3 j" w% j- a9 k
代码; {9 M y) {, Y# I3 |1 v
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素: t, i& Z# {" v; ] P2 Y
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |