下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看' H$ r- J2 g5 _. A# q# h8 q
http://www.itwis.com/html/net/c/20100506/8234.html9 _- j' k, S1 J$ Z; R
2 E, }9 ^% t5 D$ A
程序代码0 c* M( \" `# K6 r
1) public string[] Split(params char[] separator)* f+ l" R/ q! u$ ^3 F
2) public string[] Split(char[] separator, int count)3 s9 W: y9 p- r5 i' a4 c
3) public string[] Split(char[] separator, StringSplitOptions options)
' Q9 f" m9 p8 }" X" X$ ^4) public string[] Split(string[] separator, StringSplitOptions options)
' }: y' {! Y; K% Y5) public string[] Split(char[] separator, int count, StringSplitOptions options)4 e# r- x/ ]4 w9 D$ E5 s# l
6) public string[] Split(string[] separator, int count, StringSplitOptions options)2 w. ?9 k+ {% A3 n& f
. Y% l9 J+ b# t9 B7 e7 J下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
" X3 {; g# b. K. w0 Q8 E8 ]# H, f8 w2 L2 `* z4 `
1. public string[] Split(params char[] separator)
+ V) Y! X' y4 `& M3 p8 `9 c/ ~" H6 }% k$ Q/ F" o; Z
程序代码; b- x- X. e; u6 S9 h/ i. @
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
" [+ C1 o1 h, U, Mstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
/ b' |; o, p z. H m/ h0 X( _7 L$ A# t7 N- ~& z/ Q3 E
2. public string[] Split(char[] separator, int count)
8 `7 d, b7 Y4 s+ ~; t- O( p+ ~! ]( `. b: J! ~, h5 O8 x
程序代码
! `! S; {& N5 |; ostring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}; x' h% h! u; w* D1 h) ?
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
/ v4 p$ t: H( w/ m) o \1 m7 p% ~" o i( C; T' J
3. public string[] Split(char[] separator, StringSplitOptions options)
7 C" q& g; t) P+ J
1 Y% V4 I( N( X+ L% j+ r 程序代码
! X3 ?2 i! C/ ^% I: z* m% Xstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素- _. v/ h7 C: k" @
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 }" v! S. i8 ~' J; V: [
9 S# `9 Z( j+ }& ]4. public string[] Split(string[] separator, StringSplitOptions options)
- l+ g2 C |( j. T4 w% t5 b q+ O `0 R
程序代码
" ]* \( |9 i' ]8 ?3 istring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# W3 s0 S; _1 T0 s% }2 e. }' J8 [- p
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# S- B) G2 m/ `( Y7 m* p( z
/ x- I' p7 J# W- k$ n5. public string[] Split(char[] separator, int count, StringSplitOptions options), W0 V7 X& e/ r3 J' a3 {0 d/ ?9 E& ~. {
) U" D3 F- O3 Q# V) i6 S 程序代码( w; t3 H+ @- [# c8 x/ r/ D
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素* F1 ]% [5 D! c. w5 e1 W; k8 x
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( o: w5 | J% Q5 k$ F0 B- O! L
5 _+ M7 Q7 O3 P1 }2 K6. public string[] Split(string[] separator, int count, StringSplitOptions options): {0 ^; K& K' d; p. o
; F4 J* Z/ N+ K3 E6 Y9 l: p1 F8 U代码 j1 b7 _1 Z6 Z0 b1 @; E
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
4 I" N5 ?+ I$ dstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |