下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看* W' j$ G, s7 ^* R J' b8 i
http://www.itwis.com/html/net/c/20100506/8234.html
. | N9 K$ s+ i, `' Z
3 c5 l! a& S7 M! _7 N程序代码
7 n9 ^9 r% @! w. ^1) public string[] Split(params char[] separator)
0 c- c* Y s% x8 J, i- m2 p$ `; Q2) public string[] Split(char[] separator, int count)/ X% t6 Z* Q6 d, A6 g
3) public string[] Split(char[] separator, StringSplitOptions options)( C* L# N5 @0 y) d
4) public string[] Split(string[] separator, StringSplitOptions options)* ?: Q1 F- s6 O4 ]; N* X) f: L
5) public string[] Split(char[] separator, int count, StringSplitOptions options)8 p1 X+ P* W' \5 _% b
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
2 i2 e- r4 k! \/ s( W" V' c: I1 }8 Y% h2 z/ Y3 E2 k& S$ G7 C0 K( E& K' `
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):6 P t; e8 A4 k3 |
1 R# C5 O; W% V$ d' R2 v1. public string[] Split(params char[] separator)1 L* [1 h. g0 {, {$ e/ }7 x$ U
8 U9 b: _7 m5 r& j 程序代码
0 ~( t# Z( W& D" Y" |5 B% zstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}, y" e6 p9 _6 `$ W/ L
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}( A, V1 A! M5 I/ P$ L
& u$ Y" D1 I) b+ f2. public string[] Split(char[] separator, int count)
9 e( u4 J7 ?) ]9 J
5 j8 K! |0 o1 q; X/ p. B% c1 n 程序代码
f0 m4 V; t+ }! W% S( Istring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
; L- Q' _" N8 E) ?' T) }: kstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}. A. \$ e4 \. Q3 ?7 F
. J* |( [$ e2 B8 d) z9 ^: T
3. public string[] Split(char[] separator, StringSplitOptions options) E" }# j. _0 }) q7 W# P
( ^# ~* ]2 g3 g7 u- U$ B# j 程序代码7 w4 E" A& u2 ]; T4 \
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* e& W/ V: U* H. c8 A+ O
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( W& N& H4 h$ ~; x0 E+ `- `! C3 s8 `- Z2 a, ^7 ~' M+ K
4. public string[] Split(string[] separator, StringSplitOptions options)3 v2 P( p. M9 S4 n/ M1 Y ?! N. I5 B
. e6 F7 G% X' h! h4 X
程序代码1 r! G# {: P2 Q9 a; E; c1 u/ C' I
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# {) b. M8 V( n
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' y7 }1 ]8 D* O$ B, c
1 f' L2 `2 b: p! y% R1 L% H- h5. public string[] Split(char[] separator, int count, StringSplitOptions options)
( \( R5 V, G* V5 ]- g& g
) q0 f5 c' w1 |# I 程序代码! ]) ] b/ B! T* o0 ]4 R
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# |6 N! B9 G3 {) \string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
+ n3 \$ Q9 N2 w0 [- v1 ?; R
$ V- P: F _7 L& j0 O6. public string[] Split(string[] separator, int count, StringSplitOptions options)$ G4 C& g) e* \' j- U' V
( p8 M/ S4 A, U9 X4 L7 e& D代码& g# N# K$ Q, q- I1 c
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
8 R ?7 G' E! Xstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |