下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看+ P7 B9 \! W3 |1 R( y" i" W3 \
http://www.itwis.com/html/net/c/20100506/8234.html
$ a$ M) }0 q% o, n' G% C
& t. E+ i: ?' ?程序代码8 k6 p+ ]0 |* c' h4 Y: L
1) public string[] Split(params char[] separator)* r0 i! M" O% Z0 \3 Z, O" B
2) public string[] Split(char[] separator, int count)1 Z. p/ X3 G5 l2 y5 }! s* }( ~
3) public string[] Split(char[] separator, StringSplitOptions options)
# f$ k6 A4 @; `7 `& W5 {4) public string[] Split(string[] separator, StringSplitOptions options)6 x, J5 Y4 i- O" o2 o- M
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
# h$ m# W4 k0 y7 g6) public string[] Split(string[] separator, int count, StringSplitOptions options)
# O4 j8 _/ g% n& G5 {' t
5 V5 E* k: h& C. D下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* {7 y/ l" Q5 U8 f
8 W* }7 t. ~' e8 z1. public string[] Split(params char[] separator), @- H: c% w j' E9 o, k1 d
: u3 K$ x5 I8 N3 ?
程序代码
: ~' t. f) n. ]3 {' xstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
$ \( b' T1 H, `* O" g' `% istring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}8 O7 K4 {% l; V- D! q1 w
; j) K& C5 a& B2 K/ k) q4 I8 H2. public string[] Split(char[] separator, int count)1 @& |) S. s% {( q( E, G: q+ q+ {
/ a' @ m- O; Z6 z( V+ ^; F0 t& F
程序代码& J7 ], `& W0 O; M% @' c. }
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}2 u6 P; t- } x( s0 p! D
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
) Z- j) g* A- J; t) d, F W7 z7 C% M$ |. P; W0 ]; l+ x# R" V
3. public string[] Split(char[] separator, StringSplitOptions options)# u0 \8 J$ R* ?2 R3 k
7 b d3 v0 S$ o5 h 程序代码
x( S( Y0 G& W6 zstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
, Z, \( R* V. ^, O9 @/ V, @string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, m1 w7 J5 [1 y
7 v' F$ \1 b9 X
4. public string[] Split(string[] separator, StringSplitOptions options)
2 G G3 ?$ h. Q/ Y" P4 a3 h/ ]# x0 h/ e6 I. O* P* g/ G% M
程序代码
/ ?/ r" {& H" E Ystring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* F; e8 b' y0 v+ H3 L
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素; Y& P) c- Y7 x) \2 z% f @6 i
4 e) W, A: \& n& _. _$ u1 W5. public string[] Split(char[] separator, int count, StringSplitOptions options)
; P+ O" t" w. R5 [
- i1 o! f4 r2 _: F' [2 U+ F+ Q 程序代码" Z d; r% f) x9 T Z' w( s
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素0 h" K9 L0 U1 Q& B7 @
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素+ z. Q% v- e6 ?/ b
& s \# U- I" K( ~5 w/ d3 L6. public string[] Split(string[] separator, int count, StringSplitOptions options)
3 k, p7 b* c. Y7 T$ `4 s3 T: Y' i- c; y+ }; Z0 Y2 ]% J
代码
0 v* R+ Y1 d9 e- Y: ]: d: |5 J/ r7 C) T! Bstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素: C8 M+ }% X' W% `7 b# \& q, ^
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |