下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
7 I9 X8 h7 y9 M) @% x* q8 w7 l' N; W. `http://www.itwis.com/html/net/c/20100506/8234.html
& I6 ^8 [# F" O& g1 d
. B+ z5 J e8 u% }2 M程序代码
0 N3 |4 `2 K0 D8 w6 I1) public string[] Split(params char[] separator)8 Q3 D9 ?1 K { k/ r
2) public string[] Split(char[] separator, int count)
* X1 @: D/ [8 l$ W! L- _# l+ g q3) public string[] Split(char[] separator, StringSplitOptions options): W1 j1 c# u1 D
4) public string[] Split(string[] separator, StringSplitOptions options): S* l9 m0 b& T: Y
5) public string[] Split(char[] separator, int count, StringSplitOptions options). g. }, g+ i# b: T
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
' ~$ Y' J$ F) q5 e4 x4 P- M& H6 D1 q5 l3 B
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
% u. s+ w7 ~2 I _! j9 b
+ \4 M* [, n6 R# L6 D: o1. public string[] Split(params char[] separator)4 t+ {( S" B; t
' I& m V7 Y0 z$ b5 z1 }( m) O
程序代码2 u; m0 x8 u$ l a; N. h
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
1 A" h4 p( [# B4 |6 F8 Qstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
3 V, P# n# g, w, e; {9 U+ j7 C* m& E! D! n$ q q# t/ w3 a
2. public string[] Split(char[] separator, int count)) c) n) i5 z/ p
0 T/ M; C: I, h( v; ~: B 程序代码
' r2 Y( @1 T% X7 }4 \0 M4 sstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
. Q1 ^- C! F& Mstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
, j4 F& P6 M7 E$ v: k$ ?
! x" U5 G8 G7 W, O" h+ W/ q3. public string[] Split(char[] separator, StringSplitOptions options)5 c$ m4 C: Z5 t) d) j+ B- |
5 T" o3 p5 ?+ w 程序代码
) `: S- `$ X' @/ v0 hstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
; C$ z6 J* x# e$ |+ u6 @3 t pstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: c- Z$ ?: D2 l2 z& g' r
* s) q& }, `/ A$ w" Z! o* p4. public string[] Split(string[] separator, StringSplitOptions options)
* u( F0 R4 @) ]% h+ L, Y/ B& y4 W) [
程序代码
4 t9 G H6 }% s: G) Z( fstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素0 q% n8 ]/ c, T2 d4 ]. G. d' o! b
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- ^/ i9 k& Y0 R8 H9 o
5 R+ x( {5 \5 D, q B5. public string[] Split(char[] separator, int count, StringSplitOptions options)' X* K2 t6 o. {+ d, m; \
4 N# i) X8 e8 ^) v. W' g: o& l
程序代码
* N, E! g- {. V3 x' Rstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
. O7 |' o# a* ~- q) w0 ~ N* K( _string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: S) i* Y4 a# G4 Y
2 c# r0 ?3 t* e% ^& M6. public string[] Split(string[] separator, int count, StringSplitOptions options)1 M5 g+ w/ ?# T8 B1 ?. P: ^8 q
# r+ j! b. d. W
代码. P& n2 U- D* X" P1 C
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
! s: q/ k8 t, t) ~string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |