下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看+ e9 c+ [' D. w& ]+ e1 d8 E
http://www.itwis.com/html/net/c/20100506/8234.html6 A: P0 x% h, x& G% s8 u* T/ a
$ m2 Y5 x3 _; T( r* y4 l" @$ l/ |( `2 N
程序代码0 d: V! B; g: \ g
1) public string[] Split(params char[] separator)) q5 m( k0 y5 g1 \4 H
2) public string[] Split(char[] separator, int count)( K* @. b# f: X, Y
3) public string[] Split(char[] separator, StringSplitOptions options)
5 e% X1 [0 i9 `2 |9 u4) public string[] Split(string[] separator, StringSplitOptions options)
/ s2 v" Y/ }, y5) public string[] Split(char[] separator, int count, StringSplitOptions options)
( G$ I* D: a- X; ]6) public string[] Split(string[] separator, int count, StringSplitOptions options)
3 l# ~, ]7 m% P! p o) E) |
, x& q, Q& w4 c. B. y7 ?下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
) M, D- V& q2 R" R8 l: m. m7 a) ~& X: S y: S! f5 c
1. public string[] Split(params char[] separator)
% `/ M w9 r H! ]2 w6 s1 ^0 V6 \. e# |/ I, _/ C4 j, m
程序代码0 u+ d, K7 N+ x
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
' i* w. s- @/ N, R& dstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}% C: t+ M: C( o9 M: J0 }. z
( u7 q3 D; Y' j
2. public string[] Split(char[] separator, int count)
( M4 m) J1 U$ A+ V
9 c2 t1 D. M/ W 程序代码' r$ g/ J; M* y. D$ t
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}6 I, ~. D1 O1 a+ V* c1 e& n
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}# f' b' H' p* I9 F+ N9 P% f0 o
7 y& l# R4 y9 F( e% }
3. public string[] Split(char[] separator, StringSplitOptions options)7 m! l9 t# ]. D9 ~8 v) H$ b
4 D) O% _( F! @" A+ a 程序代码
" B, j) O2 q! Astring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
5 x' V# z- V" D% Rstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 ]2 ?: z; M& b6 H0 f ]( }3 s$ B) y1 L& T0 Z- P) o
4. public string[] Split(string[] separator, StringSplitOptions options)
) u+ x: ^, g9 E5 s
& z. _! y) Q* Q6 T8 x 程序代码
1 c( G: P+ ~. B- w# }string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# L: Y" @7 T9 ^/ { q
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素$ A! U2 B9 }. Y2 V$ r
7 i V1 i8 R8 q& y/ r
5. public string[] Split(char[] separator, int count, StringSplitOptions options)9 x4 V) [: z9 {* `; h, t& Z+ W
' v7 `) R' \, s' k5 N: y
程序代码
; q) k& n! c5 k7 f9 N( estring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素$ |2 V8 p& P" @
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素$ p9 e' f' q& ~+ f: w# p
# U7 l% x+ u+ H6 ] L
6. public string[] Split(string[] separator, int count, StringSplitOptions options)7 G5 {: V; l7 F' Q
$ W8 l9 o- G4 k: ?% v
代码
8 F2 c- B, Z4 }6 N- Gstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
( o% m7 S- f) A+ ]6 W" }string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |