下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看4 g; W' k9 l3 c
http://www.itwis.com/html/net/c/20100506/8234.html# _. r& ^! U- Y, C: v
8 W4 ?( `8 z% W
程序代码; c0 g- M4 c, m# S, v: W, }/ t- l ^
1) public string[] Split(params char[] separator), m' {5 a3 f: {& z: _
2) public string[] Split(char[] separator, int count)* j% m& f( a( l3 A9 P5 ~
3) public string[] Split(char[] separator, StringSplitOptions options); [! [/ \& g) S+ F& _' J
4) public string[] Split(string[] separator, StringSplitOptions options)) a& ^) X) |' [( S( t6 F" R
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
& r+ q( i' t( W% O7 R6) public string[] Split(string[] separator, int count, StringSplitOptions options); l7 P- ]/ K$ t8 G6 M* D: V
; L# ~* k- _4 g. \
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
9 @. v, x2 K* I7 i: H, H4 l% Z; P, ] ]3 R
1. public string[] Split(params char[] separator)) W+ E4 l' s& J
0 [1 u& j1 }$ D5 S/ ~# @6 n 程序代码8 E3 b! z% o G2 x& D+ h8 }6 N
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}5 B+ q, _( T! n/ H/ O! E
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}- a2 }5 t) @( y; d
1 Z. k* U* K1 n
2. public string[] Split(char[] separator, int count)' @% e& A/ H- T0 j# t
$ p2 `. n2 W, R$ x) }
程序代码
) i' S1 b- X; ?7 {/ R% Z- qstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}: F& q' N% C/ _) `! \' O% x
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}/ J' Q' B J+ ^- W: S
- U% E9 J- b7 V& ^2 p9 }
3. public string[] Split(char[] separator, StringSplitOptions options)
& O2 y* c+ P# N" J8 Z
8 b3 j; ~ u; o1 z& D7 K8 \ 程序代码. F2 \% V4 y1 K: e% A8 `
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
8 Z9 U! }' r# ]. M$ A* rstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' `# j) g. N% I5 I1 i8 D
# o6 B* ?- p* e0 w8 c1 d) i( p4. public string[] Split(string[] separator, StringSplitOptions options)
) W; t5 P. }" p# I o& e
, @# Z q/ K3 L( X4 y' q/ ` 程序代码+ P+ v) `% z( i* R, }3 ?
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素' Z4 n( {, y. Q+ ^1 i ]
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! y' }! o. r5 Q+ o# r- m
" b% |7 w$ D1 O4 ?5. public string[] Split(char[] separator, int count, StringSplitOptions options)
& [& |+ b! r! {, q3 @
: O6 p3 k! Z* S& E/ h# v 程序代码0 h. h( T4 T* V8 s( A0 s% E
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素) R" |4 t8 C# c/ H' b7 Z
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素1 A% O( Z/ f5 X( d, ^+ v& V
" h% F$ m! q2 m
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
7 x/ w! x; ~. e# C( l1 s; d5 w- S: c* i& I9 `
代码
: z" j6 [) `) p w j( n- y- T- ?+ nstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
5 s. t v/ R0 ?- E- zstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |