下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看! |& S I3 C: \9 t
http://www.itwis.com/html/net/c/20100506/8234.html
) e7 s' n( Q) m3 M! I4 @% U$ `3 f7 Y# w" t
程序代码6 b+ O/ X, r$ d( X7 D
1) public string[] Split(params char[] separator); }5 Y! z$ z# d- E
2) public string[] Split(char[] separator, int count). T* a9 X5 U0 C7 F( ~: ~
3) public string[] Split(char[] separator, StringSplitOptions options)
# d5 Z/ z3 M- k5 o1 }. B4) public string[] Split(string[] separator, StringSplitOptions options)0 p/ H2 ]& S& @- p8 x
5) public string[] Split(char[] separator, int count, StringSplitOptions options)$ a; ] ^) z" ?! D
6) public string[] Split(string[] separator, int count, StringSplitOptions options), E$ N: H+ q$ i: x2 q6 c; \2 w$ ]
8 f2 [ @$ C- k. G. f下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):' x8 Z6 `* E: \8 S7 E
# T3 x% t: q- w' R7 r
1. public string[] Split(params char[] separator)
0 n" T# R- o8 V' F G8 \/ W- V1 z' H3 m( X2 ^# e& H
程序代码
; `' z O) ~+ bstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}; O" p; Y% g0 [) ~
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}& a( z5 W& T1 C$ n$ n
7 w6 d# V( g4 ^2. public string[] Split(char[] separator, int count)
/ T- |6 v X/ x3 q& m4 Z9 i! u l. x( L! F2 |
程序代码+ ^- S- d U$ Y0 m& r/ U/ i/ A& b7 N
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
: k, Q# o* V2 }. ^ N% U. nstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}, m, E w! ^1 r8 b3 a) Z
1 h; |: ^ I8 }0 T( y9 U' f; q' s4 M
3. public string[] Split(char[] separator, StringSplitOptions options)
! p \; X( G* R
" c& f; p4 B' x9 C 程序代码( a" g" ^+ a% ?4 E8 J7 S" q7 ^3 Q
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素0 K! B5 y, N+ g
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素6 e1 o5 L V, p% F* D$ m* M
& a* S" B9 I. h7 i2 c
4. public string[] Split(string[] separator, StringSplitOptions options)" L q2 M( u& x, a: z, X
! k) n2 C [+ V 程序代码
# R/ R. j! P0 S7 ?/ w" ~. \string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素5 {1 R/ j7 t" ^& }$ o% s/ e+ W
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素( X' J- b- g0 e0 k; P. H$ a
2 C8 L5 a: u0 N5. public string[] Split(char[] separator, int count, StringSplitOptions options)
/ A( {% ~, Y: H) Y
3 [/ ]* y$ t: R 程序代码7 T; ]. `# r( U; S h
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
' t4 L3 Y& ~0 j. gstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
9 Y; B$ ]7 m* Q2 L4 s% V
6 W; y; \( S( @; i1 H) n6. public string[] Split(string[] separator, int count, StringSplitOptions options)
1 K$ M1 L7 P0 m4 N( m6 u2 `" f& P+ W' ~" W$ l- D3 T, G
代码
6 p d$ J! A0 W; G# G# Tstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
( p$ b0 A H+ |string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |