下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看7 J9 d, p; f/ ~9 z
http://www.itwis.com/html/net/c/20100506/8234.html! t3 {4 x; @' ^; x% H9 ~& |
" s& L* D# z3 Y+ w% Z程序代码( b; \& P3 ~, @# F. l
1) public string[] Split(params char[] separator) J$ A6 G+ h; p
2) public string[] Split(char[] separator, int count)
8 p4 I( K4 z0 ?3) public string[] Split(char[] separator, StringSplitOptions options)
4 w A, }- l* {/ A5 s: H& Y4) public string[] Split(string[] separator, StringSplitOptions options)0 D, T0 x& X* a
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
8 Q8 B* {' A& h6) public string[] Split(string[] separator, int count, StringSplitOptions options)
2 o l1 k \. t4 ~. W' T& Q- m3 l+ q- g1 f: w+ a e7 B/ g
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
4 e6 ^3 F4 G7 T' @, x0 J. y6 i0 ?* X d; I
1. public string[] Split(params char[] separator)! X4 H$ n3 J- X2 O5 Z
8 v7 A1 k; u. s' C$ B 程序代码
( d; m a6 R4 a+ |2 A$ l/ q" A y4 Kstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
- b8 u C8 ^4 Q5 bstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}5 B4 U$ V1 O' X: N, X1 ]
3 x- Z+ z; E$ n a; \6 N( i% z# D2. public string[] Split(char[] separator, int count)
$ u$ `2 D( x- y! |$ m- Q, B- _3 k) s0 M- o+ y9 X8 N5 G# o
程序代码8 |, i! }0 Y B2 n
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}6 W/ S7 d% J4 U( N
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}% ]( X8 y e( s; ]) J( i/ m b
( l9 ?$ H6 [1 Q6 i' b# f- f+ c3. public string[] Split(char[] separator, StringSplitOptions options)2 S/ M3 h" u7 p4 Q; _" v; |
& w0 R7 P; l4 ]/ t% C 程序代码
8 \7 I+ _) x. X% u5 ^3 G0 ] Bstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
5 B: b, g" {' @* Z' `6 w/ `string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
& S/ c. X4 t; \* ?% u( k' W2 O3 w* D
4. public string[] Split(string[] separator, StringSplitOptions options)% @/ d/ u; k) u% k) ]& t
: I) t w% d0 I3 M* a. L
程序代码
- q- O/ v# ^, M" Q9 Sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
% ~' u1 g+ c' O( Estring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# k1 Z u1 F D% b# s4 U6 @" k
) q% l$ q$ E! {9 f5. public string[] Split(char[] separator, int count, StringSplitOptions options) G E+ B0 {* J% E7 }. y
4 ~, U- R( Y B5 `5 J- F
程序代码( @6 K' _# v, _) n" P* L: {
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
$ u1 c V1 m7 D) Estring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素2 \2 V1 m8 G, y# E) x
0 h) ]! G$ i* X5 b; W
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
7 G! m1 x) m# j3 X7 b0 r, ^/ \' U% b; C* s( k, w4 h2 Z$ E* F9 P6 Y
代码, p9 V- d, ~2 R' p
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
[9 w) b/ h% O( mstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |