下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看( ]) w, \+ k0 S s
http://www.itwis.com/html/net/c/20100506/8234.html* u+ I) U' i' e; F$ I3 ?
8 J2 R, ?' r4 I; M! q; C# ]2 r
程序代码
g7 Q; ~2 x( `5 N, _: o1) public string[] Split(params char[] separator)3 d# t( B6 O, L0 ~$ O5 v
2) public string[] Split(char[] separator, int count): V2 u$ J |! s) U; R9 a
3) public string[] Split(char[] separator, StringSplitOptions options)* \. m9 l" }! \9 U8 U7 C( ^
4) public string[] Split(string[] separator, StringSplitOptions options)
: m- x [% m. j- ~5) public string[] Split(char[] separator, int count, StringSplitOptions options)
& I, D7 r$ l. R6) public string[] Split(string[] separator, int count, StringSplitOptions options)" F$ g( Y& j# y/ ]1 @
7 q" v# ~6 ^% s4 O* J
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):( N; t% m- S0 e8 _# A
9 v# r" N& M P/ Q; z6 E0 O* a* ?1. public string[] Split(params char[] separator)
: Z* c. C0 r' }' Z3 i
: q) b) s5 V& F2 ^ 程序代码, K/ u7 z8 W" T, n& L
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
; h' z+ W# \$ J( ^+ H/ Ostring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
( p0 ?- B) O1 @2 K, o3 p0 i& {
5 H, m/ z9 b4 R( c+ q+ H& f" d2. public string[] Split(char[] separator, int count)$ s, w# M7 i. U# ]% n6 o+ y$ Y( O- B
& X( N7 T! q0 P4 q6 x8 m 程序代码$ S( I3 i4 {5 I }8 e
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
$ W* s1 E# X' B R' D, m! ustring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}5 T& L& F* b; R e
" r) _* C5 l; T6 o! a3 j" v, I3. public string[] Split(char[] separator, StringSplitOptions options)
. {2 T: ]$ }3 \$ o! R$ o8 r( b( ?" o' g3 ^0 d6 c* p" Y% y6 B! u
程序代码3 ?( t# f7 m/ s& T: ~
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
- ~/ i. V! t' y/ |& sstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
" o* N7 O: k' U, e
9 m/ k* v) e- i' H2 `2 x4. public string[] Split(string[] separator, StringSplitOptions options)
8 H( w' H @9 k% p/ J* n! {5 B
7 x5 C' u. F3 t3 q 程序代码
! {" t" `* n0 w9 R V/ ustring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素8 e0 G9 |1 u1 Z2 X9 X! w2 A0 e
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
# ?" q+ v9 b$ F4 u% |* P/ D8 ]( J: G9 L8 b
5. public string[] Split(char[] separator, int count, StringSplitOptions options)4 g; J' n5 w3 t) v# D, c7 ^1 ^4 n
1 Q4 T/ `) G# C1 q- q* L 程序代码& `+ F5 Z& o/ r/ t0 C* G( h- a' n
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
1 \$ \- X) r! m3 ?% Gstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 n( {! I; |. z# F
# U. Z$ P! K v) [* j2 [ I* t6. public string[] Split(string[] separator, int count, StringSplitOptions options)+ r1 q8 a- R: ~7 J0 C
4 i. D* L, ?+ x7 _) a
代码2 K: l( x5 ~5 W
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素- \) M6 h& m) {
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |