下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看! o; w+ Z$ V3 F
http://www.itwis.com/html/net/c/20100506/8234.html1 m& c0 b/ Z4 |/ z
2 c* O% U) ]5 u+ r K5 S- ~2 l
程序代码
2 q; E" @; k. m s: l% n. X3 ^1) public string[] Split(params char[] separator)
% y& a* @! W ~) }2) public string[] Split(char[] separator, int count)
. R: W) v& V8 S: s3) public string[] Split(char[] separator, StringSplitOptions options)
6 D* ~: ?/ a# s* \3 }% ]% G, ^4) public string[] Split(string[] separator, StringSplitOptions options)
/ L. o! U+ x8 _2 R. G- K5) public string[] Split(char[] separator, int count, StringSplitOptions options)
; X% r6 H4 }3 j$ y6) public string[] Split(string[] separator, int count, StringSplitOptions options)5 T. O7 \0 B* }2 x# r
4 w- f) G3 L* u; I/ i- _ a/ A下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):; ~& W/ b$ R ^! O# q" Z3 Q
* v& B6 N7 V7 J( U* `" v
1. public string[] Split(params char[] separator)
+ P2 g7 G! r4 M6 p5 R6 M% a i& Q7 y1 Y H; w1 n W$ W/ x. x7 E" h
程序代码
4 M0 q7 i1 }# {+ e0 k* o$ E4 [string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
/ y: {# V8 {1 jstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}: P7 q# c2 j$ S F. B
% A; P" z: T! |- u- ?2. public string[] Split(char[] separator, int count)
7 Q' F: L; ~- j/ \. r4 D+ L) Y* ]6 Q s7 B+ ]8 A6 p
程序代码% z+ _% Z# n8 Q L5 |( W3 [
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
- ?! L, m6 `, h3 V; a, ~string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
0 Y& x/ K- z# ~, Q' E
; O/ m6 O6 Q. l* q \3. public string[] Split(char[] separator, StringSplitOptions options)
9 N) |# E( \1 j3 p$ y2 ^$ l5 @4 f9 ]0 l! V6 S
程序代码7 E+ @5 ^+ `% H+ x
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素" w0 V7 T+ Q/ d& @: y
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% Q( `% r9 X/ ^% X8 V8 b' O
( b+ p8 S% N! L+ X4. public string[] Split(string[] separator, StringSplitOptions options)
1 x ]- b2 `5 m- o$ H
) |# t1 F' P/ I5 P. `0 C( x4 W 程序代码
7 Z, f" W. H/ R- H* U* hstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
4 `0 S' |1 s% w, X- n, `+ sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素5 {' Z- r& t6 {' |# k4 {" h
0 k+ K8 G# k! r* V% q4 J5. public string[] Split(char[] separator, int count, StringSplitOptions options)
8 E' l F& m/ R" ^! |( x# S9 s# w) b* X$ w! {9 ]
程序代码
8 l; |) J2 S$ C( sstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素' b7 D# i4 s; r
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素6 w0 E6 J( f5 Q* M0 O) b, @) |" u" q
+ T; k/ O! g6 O' K w4 q6. public string[] Split(string[] separator, int count, StringSplitOptions options)
! D$ T2 ~! X8 L x' r, a2 Z3 b+ Z0 Z" A
代码
6 a% Z, }* e) h3 Nstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
D8 \2 W( z/ d! Astring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |