下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看# G2 F4 H& s$ h4 D, I
http://www.itwis.com/html/net/c/20100506/8234.html/ ?+ S' X1 u" `# L: @+ V
- I2 c: z7 x4 }+ U4 Z程序代码
5 h$ X! e2 t; H2 p1) public string[] Split(params char[] separator)
% Z. h2 o, h* m9 c2) public string[] Split(char[] separator, int count)
% i" f( O3 f, x& V( n$ z* c3) public string[] Split(char[] separator, StringSplitOptions options)
3 i6 ?1 b2 x2 `6 Q/ E4) public string[] Split(string[] separator, StringSplitOptions options)
$ R3 ?' m4 v" W' s* a5) public string[] Split(char[] separator, int count, StringSplitOptions options)
# H9 p4 }: e) A2 E# D6) public string[] Split(string[] separator, int count, StringSplitOptions options)" D7 f) u1 T5 L0 W: D) I6 ?
- }. |$ Y, ?( F0 f( ~0 V
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):8 V0 I; L: q" k: ^8 G0 d
: _: {% M( W: _ @: J& t
1. public string[] Split(params char[] separator) Y9 [" O6 Y2 W9 x0 `
n5 [: `' [, w4 q7 k7 r/ o1 R# ? 程序代码$ {' D; }& P" g
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
% c6 \+ d1 [! Ostring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}+ s, R/ i+ v: _& j) A; E
. [0 U4 r N, i9 c8 w/ i
2. public string[] Split(char[] separator, int count)
! N5 x4 W, ?: K% a$ R+ _' v& h% Q# W8 j4 H4 Z
程序代码
' `. b" L, a9 I# N' m9 e( astring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
% I+ q& ^, ^4 V& a+ Y) S: _string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
* ?( D, A8 G$ L& C j0 D: V5 V! j) e" g% c- [7 I
3. public string[] Split(char[] separator, StringSplitOptions options)
4 \; P1 Q' ?2 T8 k. G
' n9 b' Q- X a% M9 x 程序代码% J* D+ v5 B3 `3 r* Z4 w
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素, n3 S4 P X/ j# Z$ l# z
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素) o. G9 b* i% L+ F: L
2 l6 \" G8 ~1 @& H
4. public string[] Split(string[] separator, StringSplitOptions options)
5 ?0 N, ~# e/ ?9 y; k. X2 p5 |
6 ^4 e! B) N) o6 J 程序代码
+ r$ s, P1 `2 e1 c6 b# q; xstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素$ p* z6 o2 R+ C c7 |8 j: o3 ~
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, {, C4 C. q3 @2 z9 A
: i3 e! `! B5 ~* D- M
5. public string[] Split(char[] separator, int count, StringSplitOptions options)) s; e! d+ H% h
5 a# C0 |- p, `; c% z3 V* @ 程序代码
7 F, P7 Z3 [: B% O3 istring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素# Y/ s) B z% e4 @0 O
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素" B$ I" F ?9 ~+ v
3 n* m6 O' r& V6 Y- {6. public string[] Split(string[] separator, int count, StringSplitOptions options)
+ u0 v! B4 P6 C. ]
9 F, s" u: K3 E; }4 t% V# ?代码
0 {4 J E9 e2 k7 B( E8 dstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
; f. l! n! d: ?6 H- T. p) gstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |