下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看/ M5 X2 x* R0 d' {! R
http://www.itwis.com/html/net/c/20100506/8234.html
4 J9 m9 x) T- p* j4 y* m, i4 T8 O- K' } ]1 E5 j0 S+ p i2 `3 I
程序代码
) C0 ^. W3 | j5 V$ C' q1) public string[] Split(params char[] separator)4 I& g2 J' |& x6 C1 k
2) public string[] Split(char[] separator, int count)2 k6 R# A6 l! v$ c
3) public string[] Split(char[] separator, StringSplitOptions options)# |4 S+ U1 c' C$ v Q8 z) x' u
4) public string[] Split(string[] separator, StringSplitOptions options)
1 l3 N; s. a1 Y. ^3 ^: J5) public string[] Split(char[] separator, int count, StringSplitOptions options)1 w8 z5 {! L3 _
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
- X/ {, Z2 e) |# C: ?0 D# [8 s/ a. {5 K2 H
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):7 G. N9 j* Z3 G
8 y3 f+ R; z7 o" L/ V( M( g* @
1. public string[] Split(params char[] separator)
8 v8 {$ e2 S! M4 W& J9 U) p& W0 Q
}/ y" D& g! u3 b" Z 程序代码9 k' c1 ? f- g( B9 z9 E" c
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}1 s r6 l- ?" t! k
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
/ S5 J: t% E+ [! f
$ U% |4 Y" D0 o6 d2. public string[] Split(char[] separator, int count)
* M3 r/ e5 B; W( e0 v' H) _, [2 N4 S6 @; L# U. e5 |2 c V
程序代码& E" [$ m: Z P7 n" F# f9 u0 A
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
6 t* |' f" M- n; n3 K# a+ [string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
}. j! R8 ?! c4 s' u. S( }% {3 ^7 ^" w8 ^& u
3. public string[] Split(char[] separator, StringSplitOptions options)
' E% Y/ Y8 Z- p8 w( f% N7 m3 j* n7 Y0 a* U% n/ K, n# K
程序代码& {/ E4 A& j0 y0 {5 J9 z
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
/ [) H2 ~0 j; q' A8 B5 Estring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6 U* c z: d* q c! F4 z: T0 M
: n0 K8 Y1 v N& K4. public string[] Split(string[] separator, StringSplitOptions options)
" |9 j0 j7 h, ~
2 P' A. a1 {+ [( Q/ n/ r k5 G% s 程序代码
. H' v: _! G1 I4 N5 f7 F6 Y. P: o3 Sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素3 W" P, q* U6 o" d+ w% Z
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: P, a4 Z3 o6 A4 x1 F% P
: {5 x2 w0 G- h5. public string[] Split(char[] separator, int count, StringSplitOptions options)- V0 I9 j% {2 z6 I2 m! ]2 z+ _
& w- A6 ~# I' { 程序代码. n+ F7 I4 Y- d+ }
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
4 |8 U: X0 b$ w+ Ostring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
1 `* d& B0 f+ N
" [, G F1 a7 p- k! ~. O6. public string[] Split(string[] separator, int count, StringSplitOptions options)5 [! L3 C6 o) ?3 x
2 [0 @2 C8 l! h3 D$ z
代码
& v" `9 K" n$ E! `. J7 pstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素0 |: u" m& V2 e- G
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |