下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看) i1 z) J Z0 k' m4 E9 ?
http://www.itwis.com/html/net/c/20100506/8234.html
: X- ]5 _& H" J1 ]! n+ C5 [
9 @" Z4 {/ b9 y0 t) }1 @0 e3 @程序代码9 l5 ]+ Q% ]8 Q8 j" ^+ n
1) public string[] Split(params char[] separator)
) I& U. v! c: V2 d! J6 X2) public string[] Split(char[] separator, int count)* o, B9 _' \8 m( ?
3) public string[] Split(char[] separator, StringSplitOptions options)4 Z" g2 `0 [( y, ]; }0 v3 u
4) public string[] Split(string[] separator, StringSplitOptions options)
7 P0 z$ C- V. Z5) public string[] Split(char[] separator, int count, StringSplitOptions options)
7 ]& A7 c m4 Z8 W2 N. l6) public string[] Split(string[] separator, int count, StringSplitOptions options)% O6 D% U& J: p8 ^) B3 m
& k8 y. T, o7 L+ J! C% @下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
) A8 G7 \7 ]) _" ^
8 f% F& m' Z% G9 ]0 H; E: o* D& [2 x1. public string[] Split(params char[] separator)! b7 F$ J, N y& {0 e+ l6 q! p
3 a" m/ ]% o9 j
程序代码
. q. p! r2 M2 K9 {* Tstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
9 ?3 t. O5 u! W0 T% m" T& cstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
1 Y+ _8 X1 [) q1 U3 O& r, O" Z& v. A; v4 k
2. public string[] Split(char[] separator, int count)
! N7 L+ i2 F7 |. j8 \7 F" D, N# V- z- {/ N* n+ ?+ o
程序代码
1 w0 g+ ~, `( y) O: L% W& Zstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}; R: J F% Q- \7 j- P& F& t4 Z* b
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}4 J! [2 @- C8 Y+ c$ y
' A% K. b6 c! [3 E6 h
3. public string[] Split(char[] separator, StringSplitOptions options)7 N$ }8 m3 l, ^4 N& b& v5 a
0 o6 ?# \6 r+ m4 ]0 L2 m 程序代码
- Q3 y! I' T1 o, t/ e) N$ wstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素/ S8 ~' r3 I( R) z" H( `# a- V
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* E) y( b1 k$ y# j% {2 o: V: j$ l5 e4 |. r* Q6 i5 }
4. public string[] Split(string[] separator, StringSplitOptions options) i" `" l. L+ o! e4 i5 E }) J3 {+ t' m
+ t9 f1 v% [ N1 q
程序代码7 `' P6 _' _2 Z6 r) B
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 {( j6 i( S' W
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6 j) ~# A& {/ h8 Q
- M- i$ M) K3 m( P, `$ t- a$ ~5. public string[] Split(char[] separator, int count, StringSplitOptions options). s' \: e$ [* ]/ R. p* _; A( q& K
1 O% e9 l: L& y1 f/ o
程序代码9 V1 n+ C1 T S
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# K, e) J. Q& y& [string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, ]: X+ N+ U3 a2 B% o1 b" {
; d% ^. c& I% P: A' M/ Q
6. public string[] Split(string[] separator, int count, StringSplitOptions options)- t. M6 I% `7 Q8 V
3 I$ A: M a2 R$ }- z* ~ I
代码1 o1 h6 V" H$ l+ {5 A! O% F0 L) l- Z
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素1 c7 P8 M# z4 I2 g" H* k
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |