下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看8 j2 i$ `! j* {- W" ]: S# n! ~
http://www.itwis.com/html/net/c/20100506/8234.html/ N; `% r+ \ M( y1 p
, y. t' c$ y5 Q程序代码
3 c! u9 Y* X M4 m1) public string[] Split(params char[] separator)
9 t8 y7 I/ x+ E0 l. L2) public string[] Split(char[] separator, int count)
! k- g/ @5 l& A5 I" e6 f3) public string[] Split(char[] separator, StringSplitOptions options)# Y! K6 K3 g! B9 J" V
4) public string[] Split(string[] separator, StringSplitOptions options)) i+ z9 m' E. l' i8 g P, ]
5) public string[] Split(char[] separator, int count, StringSplitOptions options)& T+ V" J; j. \! Q) A5 Z
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
m( p% N9 }$ w7 ^; i) c% w/ ^
( ]4 Q: i& `5 K* u( Z# e$ d下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
* L# B' @; \3 h; q8 L \' v A8 R. w U1 p! i& x- y/ Q$ A( v
1. public string[] Split(params char[] separator)' w- r5 q" T. n. z) u# w: O
& X% Q. Q+ m4 h* @7 N V4 i: C0 A 程序代码
x3 c% h; _/ \. ?1 [) bstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}# U. ~+ h0 V* I) J* {7 e) B& m
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}' f) x3 S! l Z {
( ^, O% E, c) C7 c2. public string[] Split(char[] separator, int count)
! @: M% z+ A/ L& d2 x# v9 w9 Q J+ s$ j* H) |1 Y' y: h
程序代码
) E* q. v- B0 b% T8 ^/ Zstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}" n; z) m& c/ R, Y: V' M+ N
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
, {8 _( \) F4 w: L" h, J" i$ e7 c. p$ j
3. public string[] Split(char[] separator, StringSplitOptions options)- y8 R$ I2 x! f' D2 Z( i* ~, Q g
4 `: l( i; g. T- e 程序代码
9 A5 O: ?" \ D5 istring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
+ O2 H" r$ T4 ~( X! ?3 T/ cstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 V3 _6 i9 A, }" v y
0 l a, Q; O2 G0 W6 C: B4. public string[] Split(string[] separator, StringSplitOptions options)
( f; i3 w; L D! K0 P# {* o9 J) ~$ b8 y& ?2 ~' c* T& a( M
程序代码
# [) W' T7 ]3 {7 Ostring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素: d1 Z7 Q0 ~8 \- Z, F6 I
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
; A$ A6 N$ T1 |: H. y7 O
0 C ~8 T: v' ^3 N2 L5. public string[] Split(char[] separator, int count, StringSplitOptions options)
1 p B, P, o, A( h) g2 N r T. i; h4 [1 P
程序代码* X+ \- [( F4 e7 C
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素! E) M: T7 `7 y @7 I) r! y
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素5 r: [2 H. Q1 K7 |4 y: q1 p: d" e8 x0 @0 ~
' v8 I" y7 [0 [
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
) O' ~( v5 N% Z- w# u, G; F7 \" c. L3 a* g2 @9 }8 `
代码
* E$ a2 }9 |1 N; Rstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
6 Z" j$ c4 N+ d# X) Rstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |