下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看 h3 h3 F* H# m
http://www.itwis.com/html/net/c/20100506/8234.html& L( u2 f& p+ A2 y( K4 ?- |
# J' J. s) [. [- f. y" ~4 K1 S程序代码5 @' C3 j7 `5 k7 Y; s
1) public string[] Split(params char[] separator)
* N! B. n& r4 W% C2) public string[] Split(char[] separator, int count)
6 k: s& y2 ]5 U9 k5 i; o G3) public string[] Split(char[] separator, StringSplitOptions options): \7 Q0 N7 c3 q; O& |; M1 m
4) public string[] Split(string[] separator, StringSplitOptions options)" s" T- @- w; Q/ H: X* Q
5) public string[] Split(char[] separator, int count, StringSplitOptions options) R1 \: Q4 m. B- c1 P7 u" f! d
6) public string[] Split(string[] separator, int count, StringSplitOptions options)/ K8 O0 \* l p
# l$ H* D- s, G下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):5 W& p- P: [+ J4 g& m' N
& l) m7 D" Y% _5 P. a' `# a+ G
1. public string[] Split(params char[] separator)9 b( E" M- d% D
0 J/ _% q: O; A+ M 程序代码
. r5 h& V# @" x2 e4 mstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}) T8 ]" k+ x3 X
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}9 Q( j! h3 d7 e+ i
& f: n) W; i5 {5 _. ]0 E% X2. public string[] Split(char[] separator, int count)
- W6 ~: A( {6 n& P) K
/ I3 F0 s: \0 ~( t 程序代码
L, s& f$ }1 v) w0 z' Ustring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
+ h& S& Y. v* s; g2 X4 Y Qstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}$ {- b* k# O- P& n* n
( @! ?" a6 m; E: y2 r3. public string[] Split(char[] separator, StringSplitOptions options)
) L1 S/ B6 R+ ^) V$ Z2 _7 }- o8 z1 G# i$ h/ ^* f4 U
程序代码
+ G- {' d9 `& F) u$ {, Vstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
% ~! \% f6 G/ f1 \; Q! Tstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# ] r) @3 h( e' v# d. m, n; H9 o
* P" i) [; y0 V2 v. W9 ?+ v. j4 c4. public string[] Split(string[] separator, StringSplitOptions options)
8 W/ ]: n6 a7 i/ `8 }4 n/ x0 q6 z! ^1 _; Y6 h
程序代码
4 @4 G! ^# R, y4 @5 fstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* Y9 m0 Q& |9 p+ O: k" f
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素& ~8 o, S; t8 y2 u1 c$ d) v
3 W* ]- J- F: x7 s# |& s* p/ o+ Y6 t1 ^5. public string[] Split(char[] separator, int count, StringSplitOptions options)0 r+ ^' I8 g4 [2 h# q5 y5 f0 \/ a
$ [" f1 s0 ~. A
程序代码( N0 P# c, a X( b/ D& x2 L
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素* z& l% g; ^# }0 H4 Q5 s- J
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
n5 v% N' T. P) } F. L7 |& M! |
3 y0 D( l5 S5 _& e6. public string[] Split(string[] separator, int count, StringSplitOptions options)) d" \" M+ K5 c- {. }
+ f3 C6 w9 Z0 o, d, k
代码' @& ~. r2 U* N
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
3 E6 d9 [$ K q7 x+ K' t: |& Wstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |