下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看: u8 B: f3 t+ V* K
http://www.itwis.com/html/net/c/20100506/8234.html7 @6 R8 `# m4 R% B
. D" j( L K) \! h
程序代码* w R% h) h! D/ {. H7 }
1) public string[] Split(params char[] separator)4 f7 F% ]3 J$ L# ?, J0 j& k
2) public string[] Split(char[] separator, int count)
7 K% c( @( R& |" @3) public string[] Split(char[] separator, StringSplitOptions options)/ Y7 G3 H1 \( Y6 G- w
4) public string[] Split(string[] separator, StringSplitOptions options); J2 d4 M3 S! w+ ]* Q8 y. ]% N
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
0 x. s% p }! Q U8 a: T" D6) public string[] Split(string[] separator, int count, StringSplitOptions options)' P) a7 W0 {5 R/ ~. D
8 J8 V, f! E1 ?下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):6 l+ y4 s' j2 F( q7 m7 @
1 K! v* ^+ J. f
1. public string[] Split(params char[] separator)
( p5 X; p8 S; w! r- J1 j2 v6 Q% l& c4 c- |8 C: `8 l: z; \* [
程序代码/ h* H# y7 c; _9 C
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
, i( ]' K7 G* p2 qstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
4 L6 @+ C, M7 _( x( `+ b+ I; y+ B
2. public string[] Split(char[] separator, int count)6 W# g: f! c# E6 a1 X
% g/ v/ T! w3 s% y
程序代码
( I! \, }' I5 f* o- M1 k( bstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}2 A1 [' A( s2 z9 Y8 j
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
, O, o% g; H) R: ~. V8 A+ w' m& x9 w+ x& s* d. M3 D7 o
3. public string[] Split(char[] separator, StringSplitOptions options)4 t7 P: x% \$ h6 R- v, l
6 K9 n% n7 q" t T 程序代码 F P) \: f; g+ k% X
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
8 z6 G% C1 x9 N5 Ystring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素& |1 H4 a. N/ @. W2 E
& P- n9 g U2 f u4 K/ F
4. public string[] Split(string[] separator, StringSplitOptions options)
& {4 P4 Y; ~1 S0 ]: J2 p% {2 O+ |, c+ h
程序代码) z# Y# X( n" ^; f
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素) T# w$ @" y" D6 C/ }
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- e8 f* o) G' Y/ q
5 W4 t/ [3 R2 d/ j5. public string[] Split(char[] separator, int count, StringSplitOptions options)
* A' x L; u/ y$ j6 M% V! ?8 N Y* V$ f8 W/ P+ Z( T
程序代码
n5 ^+ X3 A! x0 fstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
( Z+ v% d* {* s9 K5 ]string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素) ]0 p$ A5 A# x! R& h' e; c1 `
! |5 n- b4 M) o: M
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
6 g$ d% W$ }) S4 R2 b3 j& J: D' R( S1 D7 ~
代码/ z8 F! H0 o6 u9 Z
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素) m/ X9 c6 h: R: m5 {) k
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |