下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看& \: [* @: w; v0 X2 q7 i0 I+ H
http://www.itwis.com/html/net/c/20100506/8234.html
- y! v! H2 {# \" ~+ ~7 o
/ V! Z$ ]( \- q; }2 y程序代码
' | `# O5 Z: F) {7 v1) public string[] Split(params char[] separator)2 v O. u7 D" |8 m1 e) k' Z! ]
2) public string[] Split(char[] separator, int count)6 b2 S" u4 j- G2 I
3) public string[] Split(char[] separator, StringSplitOptions options). N8 v7 J( e7 ?! v l
4) public string[] Split(string[] separator, StringSplitOptions options)
5 g# k5 _: R. [) I4 N5 _8 ~% X5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6 K; Y6 w& Y- G% ~* m6) public string[] Split(string[] separator, int count, StringSplitOptions options)7 v2 U1 n. i: R5 ~2 ]0 ]3 j
$ J. V- Z I& q) E$ h2 u+ D下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
- e, I% n% Z) l) @' X# C" H' }0 ] v$ D* h# K
1. public string[] Split(params char[] separator)* K, Y% U# G. j& |# ? l
8 f+ J. X8 [( H, u* } 程序代码- T% r, @2 s" [( R
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
5 r4 b- {7 V$ Z6 T0 Zstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}( J& g8 o$ S! Y/ W6 Y
4 X# z. q5 ~0 [$ ]( m
2. public string[] Split(char[] separator, int count)
' x. F* w2 ^' Y" C4 d6 F7 q2 x" G5 ]4 `+ k+ G; \7 @
程序代码: y! P/ _9 u0 b- G% e
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
' z# G4 f9 b1 W" }* Ystring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}; c" i, }, d+ K. l
# s: h8 a, O! Y4 N3. public string[] Split(char[] separator, StringSplitOptions options)
- I+ }) n8 [- z% @! W% B. s3 s/ s: ^ t0 M
程序代码1 \. o% [3 y5 [* t+ W( Y4 r
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
* a8 G& P- S/ n0 y- O6 }string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 }: J6 e4 U" P) Z9 O: P$ T( L0 `; s4 j; Y$ m1 t. ^; _
4. public string[] Split(string[] separator, StringSplitOptions options)
" e7 c8 K& ?5 R6 G J
$ l H, _4 X8 v0 K( q 程序代码2 f/ W7 I: X% e; ]. \
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
5 A# O- R6 S/ Xstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% a# i! x; b% T/ }# f/ R2 k; t: m+ o* A5 T6 @, ^
5. public string[] Split(char[] separator, int count, StringSplitOptions options)9 s) ?: v1 }" J* ~
) Z# @4 b+ v% u* e( u8 J% ^2 ~- `
程序代码
( b: c; ` a* I8 w+ n' U8 c' y" Pstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
+ ]1 ?, j( n. S# }% e% ystring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 |* Q7 Y2 U( U, d/ Q( r% y" f, _$ _, ?9 z
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
( g9 `) Z0 R0 D2 Q' c4 ~" A* H$ a8 `" j3 i9 [
代码
# ]* q" ?* q9 m. U! E" Rstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
! n1 ?' X6 j3 E0 Z cstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |