下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看6 b0 c% D; ?4 I! r- E
http://www.itwis.com/html/net/c/20100506/8234.html
, C# w, n: \1 {% u: `: o5 @
6 C7 K2 h/ p4 ~7 U. j程序代码2 q) C9 J( g3 C# b* l$ J" F
1) public string[] Split(params char[] separator). a0 C! A0 U3 j b# W* U
2) public string[] Split(char[] separator, int count)
3 {8 a1 S0 X1 A1 D' W5 `1 n5 e/ C3) public string[] Split(char[] separator, StringSplitOptions options)
7 ?% A2 h9 E6 R; _/ G; I; |4) public string[] Split(string[] separator, StringSplitOptions options) t9 [4 k) X! @- { N7 O
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
, t3 c! _; ~; M( V% x# b6) public string[] Split(string[] separator, int count, StringSplitOptions options)
& d5 T& G4 t% C/ u: d9 ~8 t2 p# _5 @3 \ V }' `2 z
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):% S! z. p1 a6 g# }
& `- p& g' Z! w' b
1. public string[] Split(params char[] separator)7 a0 F; I8 d7 b& N2 |0 f2 u1 D8 Q0 H
3 j% Y0 r0 c6 f9 u% V 程序代码
# a8 R1 x- J6 Y' bstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}( n1 L8 W3 F& ^3 q2 G1 p
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}7 _" c( z5 M6 {+ c, h$ E
" ~* O- l) q" }4 ?5 I
2. public string[] Split(char[] separator, int count)* c$ Y$ @. B7 b; l' Q$ c
: X! ^& Y* h8 g- |7 W6 y( i' ^ 程序代码, ~7 N9 e* k! H" U. |! q! L
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}6 o2 j, w9 ~# q% C$ G0 k6 p
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
& P) y5 V$ g# h+ j0 f! N8 D3 b# y% O2 W; u1 L4 M( |
3. public string[] Split(char[] separator, StringSplitOptions options)
8 D) a; \! i& w' I& D5 w$ G. B; K! }; X( J" b( C' Y1 {* M
程序代码
& y3 G% n! N; P$ n$ `1 Q0 q, a# ^string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素; { N. Y1 e t5 Q& o) E8 n" m
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 A9 f6 z! |# J7 {# }3 v1 o A% g5 g1 V% n* _+ ]
4. public string[] Split(string[] separator, StringSplitOptions options). T' F E$ t* E5 `5 h; f
& j6 t2 n' t; d$ E. J
程序代码
0 f$ ?3 w4 Y7 b4 Jstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
6 J1 k- [$ a0 ^- m2 Q0 Wstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素5 z8 x K# O3 M# D, @
' R# d `9 n A5 H5. public string[] Split(char[] separator, int count, StringSplitOptions options)
& e4 x: ?$ S7 e% v% x7 }- Q
6 c! W' h1 ]- Z 程序代码, ^ g. f% |4 C2 Z: g) @$ y
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素# }) M1 ^/ `6 [4 m! S
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素$ b+ E. Z t; b4 L, Z% u
0 L! g% x3 j( h
6. public string[] Split(string[] separator, int count, StringSplitOptions options). ]5 C/ A9 f$ L9 A. Q
4 e% v- P* D5 H; E, y代码% d! j- l! \. f N/ H+ a- W- ~
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素. u, v0 x* U! D. a: }9 D9 ?& J
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |