下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
a6 u3 ^8 _# T( _http://www.itwis.com/html/net/c/20100506/8234.html
' i8 X. o4 V5 G4 D5 g1 L
) ~! a9 u5 N2 n# @* M4 L2 m程序代码- g% v c* q' G+ ?+ c* A
1) public string[] Split(params char[] separator)) ^2 v. s" n' ^4 }! C5 w" ?
2) public string[] Split(char[] separator, int count)6 Q0 k4 W+ E9 i1 G; Z2 j% I9 l
3) public string[] Split(char[] separator, StringSplitOptions options)
% X( b; D$ t$ u M) ?4 w" K4) public string[] Split(string[] separator, StringSplitOptions options) |) f7 S1 r) R; d+ m
5) public string[] Split(char[] separator, int count, StringSplitOptions options)$ K1 C8 B2 r3 S, ~. K' W# ~; m
6) public string[] Split(string[] separator, int count, StringSplitOptions options)4 O( n! F5 L2 K7 w% @) d4 F
6 B; C0 _; u7 @5 I; a0 [+ A% N
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
+ M# V# k: e, }+ S; f
" n% W# o+ {: o( [6 t) [$ y3 y# Q1. public string[] Split(params char[] separator)
. c2 n) m3 |! R, ^3 J& E2 p3 E
0 s- c( U2 z$ J2 B$ W6 ~ 程序代码0 `* @' H( i$ t5 C5 R
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 c8 |3 g1 ] f8 ~. e
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}3 v Q8 B4 B. q) W, y
, R# S% T: M+ Z; B+ n2. public string[] Split(char[] separator, int count)
% n5 H/ c: _$ m2 w7 q$ g" ]; W! Z! P- g g
程序代码! j e6 w7 x, R1 _, {
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}, H* j( ^; R# b! f- D" y. m# h
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
7 `1 u) Q( k u5 `( V
$ g3 S0 V3 W' B1 r$ k% V3. public string[] Split(char[] separator, StringSplitOptions options). k( ?( }& @9 s3 @* b
/ O+ C) J0 N9 R& X3 ? 程序代码
" p, B3 Q7 M$ I4 T+ ?( vstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素; b' n+ {$ Y# m0 f& |
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素8 g3 G# B$ c! t
, g- w4 `- a; S$ k( j* y( m
4. public string[] Split(string[] separator, StringSplitOptions options)) T) j# n% l) k( {( {* m0 R
, l: g' Z K$ l7 t: c
程序代码
# d: v6 P2 _: Gstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
* s4 Z" M* x Z& @string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素: }' j% x4 T5 `- {
/ s. k- x0 }% ^; G& P5 T+ g5. public string[] Split(char[] separator, int count, StringSplitOptions options), J \ G: v4 l) A2 [% T. m
, u9 ], r$ `$ N0 O+ i5 w3 A
程序代码
3 h) N' _' t7 u5 a7 R% rstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素! d, o+ N" j P1 V
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 G& m# [% m" h7 [" x
5 q: w8 H) ~+ W, N6. public string[] Split(string[] separator, int count, StringSplitOptions options): t6 X# E5 f" p) F9 U8 ^* |
3 Q6 Q7 ]( Z* w代码& v+ {( L+ B( R1 _* L: x3 H
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
1 F; ^) x7 I9 Pstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |