下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
: ?2 h, G6 T) X; k$ Dhttp://www.itwis.com/html/net/c/20100506/8234.html
4 J$ v+ G. l7 \ ~4 X; P1 k2 h. g& p1 o& l; S6 s
程序代码
% t2 U G; T; v# Z1) public string[] Split(params char[] separator)+ H( Q( v, F$ C- H/ }, a
2) public string[] Split(char[] separator, int count)
: c% r/ G) ^$ c) q8 a. ~/ Y3) public string[] Split(char[] separator, StringSplitOptions options)
0 X0 D* P) ~1 D' x4) public string[] Split(string[] separator, StringSplitOptions options)2 ?/ m: S, y0 p' ?/ t
5) public string[] Split(char[] separator, int count, StringSplitOptions options)" P! C* W" K* N! y& O2 g
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
3 y! p5 H: C+ t* u9 O1 `7 ^1 c0 Z$ Y( `0 K( Z9 Y! N
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
$ h* d7 O$ q" N5 \5 a# Q
9 t/ j; w' t% J5 M1. public string[] Split(params char[] separator)7 f- c+ K' q5 T- {- b! Y% p# t
. c% [. B$ b+ x# i, r. J, }2 C 程序代码
8 ?2 O3 \' W8 l+ R; C8 I1 Xstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}" e& x x3 X1 e$ K5 ?
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
* _- C5 r) r5 ^; X- U0 j+ \2 `8 u; S7 ]
2. public string[] Split(char[] separator, int count)$ D+ N) D) r0 I C
3 j! j/ U: q( t4 w2 C Z
程序代码
( b+ c. | u' W# y Y8 c+ ^+ ~string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
1 W2 U- F3 X% A8 Jstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
) J5 p; ?; u @0 X5 {- `4 f! e, T& q5 Q- J9 j
3. public string[] Split(char[] separator, StringSplitOptions options)
+ g0 j4 D/ v! ]4 z% v+ l0 ]( S2 W. A5 O9 `! ]8 S: `" E
程序代码/ Y8 i# @( R3 e8 C0 G% s- U
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
1 D" Z- h3 G8 f' M5 wstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素2 i* X- S* Q1 P4 C) s; K$ s1 t" @
4 z1 Y) X& A2 R; e2 ] o+ F' ]1 {
4. public string[] Split(string[] separator, StringSplitOptions options)
4 z7 ]: g# z0 Y* a# a5 l+ j0 Q7 p1 q) t
程序代码# f; O5 ~8 L' t8 a
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素: x7 C! v3 ~: D, w7 {
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素% k1 j, L7 F8 d) z) }
# w6 w7 f& E' D% }' i/ ?* E5. public string[] Split(char[] separator, int count, StringSplitOptions options)0 y5 x- y( z' i% B
y9 w2 w& D4 m3 K: ?
程序代码$ v0 G8 z7 V, ~% o7 g7 R
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素8 X" ?# x& b' ?7 q1 I! }
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
O+ d# g3 Q9 H+ @# r" [( y
( k& A5 d! z8 Y% ^0 E6. public string[] Split(string[] separator, int count, StringSplitOptions options)
/ Y( A) x2 ~ B
$ p' T# G2 R# a( h代码6 |+ M% a/ E2 y8 H/ B6 [
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素6 c& V. E( \% }9 P' A# S4 Q( L' o
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |