下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
+ V8 N0 E$ m" n5 b, P% Khttp://www.itwis.com/html/net/c/20100506/8234.html
* m2 o/ b6 S; o% M; a }- [% \0 m& n6 z# [& g( ?" ]4 N
程序代码! R" I1 S3 b$ P; o2 ~9 y; H
1) public string[] Split(params char[] separator)% o$ l2 ?9 m7 v0 @, w! p' b/ @1 a* X
2) public string[] Split(char[] separator, int count). ^" ^, f- H; y: y8 J* a
3) public string[] Split(char[] separator, StringSplitOptions options)
1 z9 N7 }3 V7 Z& U* f ^ n4) public string[] Split(string[] separator, StringSplitOptions options)
& y; B2 M. |8 }: L8 M+ B5) public string[] Split(char[] separator, int count, StringSplitOptions options). ~( c: t8 y; c" z# O$ @
6) public string[] Split(string[] separator, int count, StringSplitOptions options)) {8 m6 h8 x$ u( V7 ~
% x0 i+ K4 m4 _* ~ I8 X9 S" ~下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):' M2 C' B6 j& a( y
* w7 J0 y- K: O H: ]
1. public string[] Split(params char[] separator)0 R1 D& |( ^8 F9 r: v8 E
, A6 x% h6 K8 q" |8 O( s 程序代码
& c/ [& H+ f1 a& v( {8 R0 Mstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
2 q! R' a. Y' v, B8 A0 B3 rstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}# h0 O4 J' C$ B2 `8 ]3 r
/ E! l5 o) {, M$ ]* O' U5 N& u
2. public string[] Split(char[] separator, int count)% c! P) z5 K6 O3 I7 s
, B" `, j* F+ a* i [ 程序代码2 n7 V( S* C/ e
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
( @$ A: u: D! J* \3 M# n% Gstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
: x0 l" }4 R$ b& p* b' u6 V; L
: M6 N) Q7 K: R5 ?3. public string[] Split(char[] separator, StringSplitOptions options)
5 ^) `& z' ~& W3 b3 P. @+ t6 e: U8 m7 J
程序代码
1 \- ?" X2 N+ L3 R# H4 H8 qstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素, e, q! ?7 \! A( D3 b
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- ^7 i3 N. A) {2 q. h9 _8 o# Y+ x% u) ]
& w" ~ e" F* f
4. public string[] Split(string[] separator, StringSplitOptions options)
) A% q0 l' I2 N& K$ D- C9 |; G) S3 E# u( p
程序代码
1 I; j) ?* v. K* @0 ?* Ustring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素3 _: t* F7 G8 z
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素4 f1 r3 `0 p8 M& Q. I
) \, s5 W; Q! }6 K" `5. public string[] Split(char[] separator, int count, StringSplitOptions options)1 W( F4 G6 \ x) D
" I$ }. n: P% x2 R3 t 程序代码
# L1 \- n. B, Q \6 k+ e* G' Vstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
8 V. b5 n2 \' l' _string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素. j* I# U9 A9 }7 m: r' Z
: v3 b" v2 `5 |- F
6. public string[] Split(string[] separator, int count, StringSplitOptions options)$ T" {) K0 z9 T' Q
! j$ S1 i1 B0 T( [4 Z2 b' {" l. Y
代码3 g" E1 k2 X/ ]
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素1 Y3 T( ^, R! W/ h
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |