下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
6 B ]4 |5 T5 ~& ~http://www.itwis.com/html/net/c/20100506/8234.html+ _$ ~& w) Y& _& O! K
9 U7 r+ f* O0 E' K. J) A) c
程序代码
" x! a! `3 \: X, Z& `6 n T+ L1) public string[] Split(params char[] separator)
* [6 q" o q* Y# v: V9 C2) public string[] Split(char[] separator, int count)
5 h; }" T7 U7 K' `3) public string[] Split(char[] separator, StringSplitOptions options)8 b" h; C' h5 y2 k' x' I
4) public string[] Split(string[] separator, StringSplitOptions options)$ _9 ?, i1 T& q1 T$ ~" F5 i$ ^' N
5) public string[] Split(char[] separator, int count, StringSplitOptions options)/ g1 ^8 w4 F2 m% r* e/ J# J
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
/ x! s1 n7 _7 ^1 O. e0 j# n7 N7 z% E8 h1 P
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
! _/ H9 x7 `5 c7 ^8 Q( T, Q! H5 e. V2 r3 m7 }3 j' C
1. public string[] Split(params char[] separator)
! z* m5 F2 V2 a- U% z- S* n7 X; d8 r3 j1 Y6 Q
程序代码
% n# h& n5 Y! S& @- V Gstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}: b! j: C. m( W- Q
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}% j' ^' M: `6 e
& A! q4 |% p, ~8 z
2. public string[] Split(char[] separator, int count) y+ l8 ^; I7 m. Q& l# K) J0 S
1 Z: ?, f2 T$ ^) K( ~7 m 程序代码
5 r4 [! A2 F/ ]( m4 hstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
: U! F! {, R3 {7 }+ ]string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
( L4 M$ b% b D4 H. D7 k* u( m- A; h3 L; I& r
3. public string[] Split(char[] separator, StringSplitOptions options)6 n# a& S3 y- t% R. F
8 W7 J1 H- L" t' E
程序代码" o) E$ ~% H2 ]2 f! E. J2 Y( l
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
) `8 t. {8 a5 a) x0 Hstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* t, g; i3 l7 R8 V$ C7 U
: n! L( h$ {/ i; m# A) V4. public string[] Split(string[] separator, StringSplitOptions options)
+ k! _- i* G3 N+ d- n* @
, r, \) [$ \' [3 [5 Z. L) N 程序代码8 b. C4 M7 Q. s0 E
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素% |; V/ s; s9 x; Y9 a5 x) u
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 B8 m& M& h$ r" n
, e, g8 I5 A& J: i; t; G4 z5. public string[] Split(char[] separator, int count, StringSplitOptions options)
0 \) \0 V: r, ^' I% Z2 o3 b5 U6 Z& ]4 |2 H4 p2 n3 f8 C
程序代码" c' m1 D# t8 F. b* l
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
) Z# D+ E2 d" c% Y, o1 I. pstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' s. l% d9 \, [ I2 B1 _& Z8 E" ]7 V- @0 q. I# M
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
+ R. O, [) v+ L& i; }7 I- F9 w4 X0 Y3 t: d2 K+ s+ z5 f
代码
4 n4 G, X- l5 w1 r; J4 [string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素2 N1 l1 i* K8 u1 G7 b
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |