下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看/ j2 V) K/ a) n) n! {, i! m
http://www.itwis.com/html/net/c/20100506/8234.html i! |* C; s# @
- U& G) t. ~9 |& E, O& X程序代码
) f) L: a5 \# S+ f' d3 b( R1) public string[] Split(params char[] separator)$ C! J( P2 P+ B8 o k' m; d: ^6 j
2) public string[] Split(char[] separator, int count): _9 g6 o" H, h
3) public string[] Split(char[] separator, StringSplitOptions options)! Y- V$ o. I1 V x# k! U
4) public string[] Split(string[] separator, StringSplitOptions options): o- \5 I6 p u# C `0 b
5) public string[] Split(char[] separator, int count, StringSplitOptions options)% ]- s3 g: K6 c. E G
6) public string[] Split(string[] separator, int count, StringSplitOptions options)0 H* w q8 e- S5 z! s( q( ~
0 O& u# j% P2 H, R- y. G# f' G
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):: H) ~# Z' v* ~$ L; C, @$ K. B
) g5 R# l% c+ S& e) [
1. public string[] Split(params char[] separator)
, l: ]9 J) _! |1 e. o# `1 r) d' }2 A$ ?) b8 k' B) _8 } z' G
程序代码 b- I$ y U3 t4 R- A0 F
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
% h' D) m+ C( B' E- W" F% Estring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}+ I0 V/ k9 ?* }3 b
& @ M( ]5 I/ m! `9 G
2. public string[] Split(char[] separator, int count)7 u G g( e( x2 X0 r( D. \
# Y/ r* M" q. M. W7 B
程序代码
! [4 ~0 A; D, a5 O$ D7 O7 l$ A7 Rstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
9 m* G* m3 f. p: [: } a4 Q. bstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}& a( I. N5 f' x8 e& F2 d( g
! U& F& D4 s; o6 Q; r, W9 I1 P
3. public string[] Split(char[] separator, StringSplitOptions options)( A5 h1 ]; P. [& k8 w7 ?& N5 L
+ J/ _# _- w8 m) }) t7 x 程序代码
) Q, x5 ~, U' I) f+ H0 ?( Mstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
% ?! ^# u. u; f) D6 X. wstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素. H3 C- u6 u9 N+ J
- Y+ S) l- V& Z/ w2 U. y2 ~: t+ E
4. public string[] Split(string[] separator, StringSplitOptions options)
8 k4 _* {: u1 _4 O2 O
c% b1 V4 Q/ ]0 S) z 程序代码$ G2 d' Y- Y' H& i5 j
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素! D5 `+ m D3 m( d# @
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 g: \* w h& j" |9 ^. h4 ]3 Y3 g6 {1 o5 Q5 b" G% W
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
6 u, f- Z; f7 [/ L
) I( h' {( P4 D7 Y+ m! @ 程序代码& z4 ^# f- |& v3 G* s0 m
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素" i# W- f! b- ]1 _# U
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 V7 [7 | N: ~5 P
; q; M& b# N+ f6. public string[] Split(string[] separator, int count, StringSplitOptions options)
2 f. N/ c8 z. a" @1 `8 O1 c: S7 I6 z; b B9 d9 E& ]4 S
代码
, \; _$ p. W4 S2 u/ l* hstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
" `, H+ D, P% j* l- w3 a' w& i) Qstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |