下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看) R6 Y U) c% [9 r( o1 q
http://www.itwis.com/html/net/c/20100506/8234.html
5 i F, B5 e: H- X, J
* `: h' c! u' g; h7 q; N$ Y" E程序代码0 `& y* C- K8 g: e' a- w' X
1) public string[] Split(params char[] separator)9 \5 }& `1 n! j" y" K- I- s
2) public string[] Split(char[] separator, int count)' G2 F8 e; q5 v+ ]
3) public string[] Split(char[] separator, StringSplitOptions options)
0 s% Z9 x* E2 e% W4) public string[] Split(string[] separator, StringSplitOptions options)0 @- {+ w' c$ O- O% E# q
5) public string[] Split(char[] separator, int count, StringSplitOptions options)) N$ B3 O# ^) ^; a
6) public string[] Split(string[] separator, int count, StringSplitOptions options)! A" G& h" s, E( M4 y7 H/ {
- R! C- X- v9 i' }0 t! G6 P7 M! u下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
& h- m) Z! o- d1 r1 z# s9 Z. O; L- @1 p$ s! m9 ?
1. public string[] Split(params char[] separator)
* H# J& Q V! v# l1 ?! W( E8 }8 u0 H
程序代码' v2 T N" x8 U$ C
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
& H3 s3 ]/ s: F5 q' T ^string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}. [- }2 u' j( E( t' B2 o7 Z
( J' m k X! n: T2. public string[] Split(char[] separator, int count)
& [7 C f( v9 V3 g+ B7 }9 d# x' H Q2 Q* {
程序代码, ~0 O4 Z; R$ G5 ?
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}% j- E6 s. G0 Y4 V- B; ^
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
" g$ q8 k# K* O6 w( }: k: p% d* w/ C' z8 P+ Q& o" g$ H4 D3 Z
3. public string[] Split(char[] separator, StringSplitOptions options)5 G1 d# _3 a c
% O" o% V- g( Y1 G) M: x
程序代码
1 {. C Z y/ |, S/ C, Istring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
2 o J2 A( b5 `: l$ C0 E' xstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素1 [+ O) ^& t' O. l$ G
( w& r6 F3 C9 Y+ C9 v+ N) K, _. ^8 r/ D
4. public string[] Split(string[] separator, StringSplitOptions options); y# }. v/ w' f
) L9 z& ~' r' N; i" j; i
程序代码
1 Q2 v5 B! c" Q; E7 S6 C& ^4 n5 sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
, r# M5 ~) W1 h! D/ F0 D8 estring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 p# e7 `! A( K9 G# L& i5 l# S0 ?' A' @3 u. T- s2 D3 P
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
" ]/ s$ J6 y6 S4 d' ^$ `* [/ ?* O; {) Q) R2 s
程序代码& k' A7 R4 b' F8 i/ J1 b3 X( `0 @
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素' U4 _5 a2 N) T7 z
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6 Q( m1 \( i8 m/ T6 S* g& }* i9 s' j' w$ s3 h5 ?" r7 d! M
6. public string[] Split(string[] separator, int count, StringSplitOptions options) Y- I/ L3 \5 z' h* g" Z! v
- y; `6 c0 o# ^% R& N( @代码& r- Z& X6 w# i( g
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素" h# ?* Q! a; s& T8 ~1 E
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |