下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看0 u* [9 v8 H7 g$ `1 ^
http://www.itwis.com/html/net/c/20100506/8234.html3 E1 i9 f) ?8 v/ q/ p
/ L6 q2 v0 }4 o2 M+ h/ L0 p程序代码2 A; V5 V& H0 p8 M7 i
1) public string[] Split(params char[] separator)# C! D! ?& Y; A) o) B* A5 ^, A7 N
2) public string[] Split(char[] separator, int count)" v: x4 x) Q; S- X* \" d4 e
3) public string[] Split(char[] separator, StringSplitOptions options)
2 ]5 ^/ _6 u; N: U" [4) public string[] Split(string[] separator, StringSplitOptions options)! S* x( A: {% z
5) public string[] Split(char[] separator, int count, StringSplitOptions options), N3 x) p; {; s, n' |( ?
6) public string[] Split(string[] separator, int count, StringSplitOptions options)' G/ @' x. _0 D6 B- v N, ^$ p; @
/ n1 x1 H' U! B% `6 K1 `
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
! @: a" _ ]0 ~: ?- t9 A% `7 B$ `$ y) X1 K8 @. F
1. public string[] Split(params char[] separator)$ Y& ^8 q, k' e
' R: [. ?0 I1 M 程序代码! a! ^$ u" V. P+ k/ O) @
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
. x1 B) a: ?* Y! Q0 W5 o5 J+ Dstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
8 r3 b% r6 M" r- _$ L! i: T X3 n2 E! t, A9 F/ V2 U+ y3 @2 r
2. public string[] Split(char[] separator, int count)' n+ Q8 ]: @7 S0 k# `: G
2 {+ J# }: ^6 q4 f
程序代码0 I1 e$ Y* n e; U& W% o0 Z
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}0 I, j( C! B7 R" [
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}$ ?6 }' s& v2 g6 U; A
" J% i @" q2 N5 [6 O" |
3. public string[] Split(char[] separator, StringSplitOptions options)+ |: F1 j7 D$ q, c, m5 N8 _+ b
6 e! \: {# d, p* p
程序代码. J: @5 {8 U7 t$ u
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素; d0 {6 @9 o% R; x& Z# w
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* N3 h. r, M( a$ z
. x# C7 d/ o2 h4. public string[] Split(string[] separator, StringSplitOptions options)
) ^5 ?) U- U, L q* d' H. H( Q$ F' T8 U
程序代码
& W8 b: o2 v! V4 o5 Dstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素. ^! H0 z5 p: P7 e; k# H
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- r% B6 k/ j& ?0 U! h
% K3 {, j5 L& T" T. m
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
' Q0 G+ C+ R& F5 t$ [* O; t$ H
1 {! T. v9 ]9 l) A) N* P 程序代码6 T1 w* P& i$ ~# x! E m
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
- _" N9 l: f5 W. f3 ^9 C2 {string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 I! y1 M4 E, B* F M# R) b5 B3 p0 s8 c5 `3 q) q! |, i1 E
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
0 B" S6 P; S/ |( o; \7 y
( s) j% Z9 b) P# x! {, m; n X代码, T5 V; v2 Q9 J, V' O: ]
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素8 ]' q) U; X6 {0 B+ W* q8 v5 C
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |