下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看$ T' d7 a' y' ?8 h( f
http://www.itwis.com/html/net/c/20100506/8234.html
5 M' B7 C( G: ]# }1 G
; G+ r. U! S3 g/ P* [程序代码( ~5 \( Z( m7 G; K/ C: p" i
1) public string[] Split(params char[] separator)" ]5 T/ a2 y! { N% a
2) public string[] Split(char[] separator, int count)
3 ?( H, O; k7 Y3) public string[] Split(char[] separator, StringSplitOptions options)* N) k, D( M- Q) }; o) X( t
4) public string[] Split(string[] separator, StringSplitOptions options)
1 D9 u& B) o* O" s+ V5) public string[] Split(char[] separator, int count, StringSplitOptions options)
) i. O2 _8 p, l6) public string[] Split(string[] separator, int count, StringSplitOptions options)! r3 r, ]4 P5 M! k* h8 \2 ?1 ~' B
. |4 o# ^- l% T [) ^下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
, v: Z9 v( K3 T; j: f+ [' Q6 ~3 o" \& n
1. public string[] Split(params char[] separator)& K6 B( u! v6 s) z- T3 q, S' n
( [. w/ T5 i Z! P9 ~) E% s
程序代码6 x5 g# e! M4 D9 y" M2 H
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
* }4 W" S. z+ ystring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
6 }, W' d7 e! W( F1 Z& m% r- H8 d2 E, R) j7 i6 Q: f
2. public string[] Split(char[] separator, int count); K5 F7 e5 [; s8 {3 I
; {3 D! N# P8 L- w# m
程序代码" T' j4 t8 }! n" ?* J5 A' ] T
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
% I0 g2 `/ S5 \- e2 n9 fstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
a% k; E. }- O8 M1 B! Y }4 [7 K" f" g
3. public string[] Split(char[] separator, StringSplitOptions options)
; g7 o) n9 X8 V ]5 c$ I7 p' Y! a w- R3 g' q
程序代码& p% w3 d' `0 _$ l) k) m2 U2 W
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
. B; ^# R# i' s! T8 W% ^) r$ s% y% ^/ Astring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素0 {9 W( p, t4 x' T, c8 \# K
6 F' n+ l1 x* e" d4. public string[] Split(string[] separator, StringSplitOptions options). ^3 Z: |3 q7 v* ?- F
3 o; w" b& d4 E
程序代码9 U d' ~7 \/ C) g
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 e4 n. q" c& \2 _; I
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! v% T$ F' f1 `3 p+ H
- q7 L$ A h8 |& Q2 ^7 ]5. public string[] Split(char[] separator, int count, StringSplitOptions options)
) r/ F( @7 `) ^# A# ?3 s1 m# x- ]( \
程序代码9 a" M& z' c/ ^4 ~& B L
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
% }" E1 v* g) zstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素+ K+ m5 A' a' J. f: ^$ R3 E
; i* ^& @+ @* F) Q% I
6. public string[] Split(string[] separator, int count, StringSplitOptions options). _" J" t' s) @2 o
1 ?1 h1 R6 z8 O: o1 ~' {' o m代码9 C0 Q# z- M$ N y
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素1 O% l! X. N l y. \, |! f& e
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |