下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看! S# b0 a$ x1 h5 s5 |- ?) K4 h
http://www.itwis.com/html/net/c/20100506/8234.html0 ^. B) F9 N4 J8 v6 A+ ~8 R
% H5 z% Z6 x5 h7 c4 v1 \
程序代码
5 i4 m1 v' b5 o" \1) public string[] Split(params char[] separator)) ^8 M' ?' G5 [
2) public string[] Split(char[] separator, int count)
) {5 ~7 I. A1 Q I2 c' O7 p v) o3) public string[] Split(char[] separator, StringSplitOptions options)! e, t7 _' L$ Z6 t. `
4) public string[] Split(string[] separator, StringSplitOptions options)" t1 {" h/ d u2 @* S0 r
5) public string[] Split(char[] separator, int count, StringSplitOptions options), W0 s- P M( S5 }1 s: ^: Y0 G* \, S
6) public string[] Split(string[] separator, int count, StringSplitOptions options)% h& H+ w6 p7 ]) K, q; n3 \
: \4 j" U* Q7 e) Y下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
6 q2 p, [9 |5 b& g8 [: b' R K! Z' B* K) H) m
1. public string[] Split(params char[] separator). y7 E7 S) P+ Z; b
8 x/ }/ Z5 W3 {" y9 }8 P4 I% t
程序代码6 O" y# i) \; D |: ~
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}" } b- o* Q2 d0 `. ]
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}: I% S/ Y1 I6 c6 y) a% H) ?
" j6 |$ A9 |2 d2. public string[] Split(char[] separator, int count)! B6 ~+ q1 w% V# u/ @& f
& C2 Y$ D( J" z
程序代码
% }/ {) P0 B* v% [/ `& b( kstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}( |: C# C% g4 Z) {+ }) a
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}5 ~( a9 O7 o+ U: J3 _. N$ L* R
4 D4 ^0 d. N! ]. l
3. public string[] Split(char[] separator, StringSplitOptions options)
( t4 ^' v V& E$ c% j# Z+ y
/ e9 S8 v8 q0 O! g 程序代码" o# ~7 J) _* A1 X" o9 z
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素+ G; u9 w! m, |/ l/ g" C
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- u( I$ ~9 E* Y; l9 K5 t8 c( M
4 v/ C. U5 N- L" x9 Y' O
4. public string[] Split(string[] separator, StringSplitOptions options)! }. ?; t$ u6 L( ~
8 N7 m4 v- ]; _& w/ n 程序代码
\: c0 B' I- w( v# O: astring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素; @/ r6 K7 Z" U% e
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ ^4 y5 o8 Q4 E" W
$ |3 B/ t5 Z4 j6 q: n* @1 O5. public string[] Split(char[] separator, int count, StringSplitOptions options)0 q+ K1 h7 W5 j) k. H5 g# Q$ L
0 J7 k+ J; V8 r3 l0 Y* S! |- g 程序代码( t4 m$ ?( G* w9 T q
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素" y9 _* Q% o1 e
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素% X5 g/ A; ~( H- ?
2 m! y' Z0 |9 o" `/ M+ Q8 m# E
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
; F4 `% m/ g5 a$ H" L- ]: _+ j: {
; v" D! e: ^3 U4 @8 P6 [代码
% }) F Z9 R* h$ J( A+ gstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
, N% t6 y* R! Xstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |