下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看7 P8 n. {7 V+ q k
http://www.itwis.com/html/net/c/20100506/8234.html
2 R0 _$ Z. Q4 G& B1 w- b5 c- U7 V" D8 l3 B# j) o( F& ?
程序代码
$ Q" p' O0 ]0 \4 T4 n1) public string[] Split(params char[] separator)& E. I- W( D! B* X
2) public string[] Split(char[] separator, int count)
, F/ O( ]7 o; H9 q1 y$ x( b3) public string[] Split(char[] separator, StringSplitOptions options)
3 e/ V) G- m3 h" y. T4) public string[] Split(string[] separator, StringSplitOptions options)9 |( F7 W1 S4 ~, i) `5 Z/ a1 l
5) public string[] Split(char[] separator, int count, StringSplitOptions options)5 m+ z; X/ l8 v D
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
. X1 n# M' c) H) M
7 |; O" q+ O! ?0 c下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
+ U- F/ U2 p6 o, P I! C; _/ s
: S; _6 Y9 f, K+ T: Z* e! ~1. public string[] Split(params char[] separator)
- ?1 l) u' W7 h' t( [/ H
0 s0 T3 k2 ^# F; z 程序代码4 q; l* {6 c3 Y8 r. f2 _, |/ v
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}' b7 d) e4 M5 G0 q- T, S, B
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}2 d# }7 O& C* Y1 ?6 ^5 q! S
# u Y+ g0 t- P" v; P2. public string[] Split(char[] separator, int count)
3 I; u9 J! h% j$ i6 a" g+ {* M1 a) _& c# o9 v5 y0 i
程序代码
4 K: n9 q: U# i4 Pstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}0 u; K6 y$ s0 E5 U# h( }, K& R; b
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
- O# ]( e4 I7 u" D, f6 Q, l# g8 @& b5 O
3. public string[] Split(char[] separator, StringSplitOptions options)9 Q- L, {4 |! I8 v
& O& \! v' E% v3 H+ k) P
程序代码
* ]1 z- N' ]: g; w# g2 L2 rstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
0 b2 n/ g. V- N8 R# `string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素1 C( m8 n9 P& u! m
/ t& z# J5 ~) @- W, I
4. public string[] Split(string[] separator, StringSplitOptions options)
2 i+ u3 _5 ]/ j% q s
) F$ W9 ~( F6 A* b* g 程序代码) V& h/ r% L# \# S
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素! V- X! d$ J+ O w0 h2 S3 r
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素2 A4 [& k& w3 f3 P5 D" w
, n/ C5 y( B4 m( q' b* k/ B" X
5. public string[] Split(char[] separator, int count, StringSplitOptions options)! V7 J, r2 l' V9 n( T" y2 n* Q/ p
. c' q. f9 d9 \2 u- B
程序代码
- S) L+ k9 G1 K$ [1 z! Nstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
6 W6 X6 P5 ?+ i0 Vstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 x1 p4 I& u8 i) D
6 \, ~+ u5 Q, [6. public string[] Split(string[] separator, int count, StringSplitOptions options)
. [9 p# q) E2 {0 u( x& o! Z
$ ?' t3 A5 M7 K! ~/ c代码
0 n6 `% t+ Z2 ~$ k0 n' m( Kstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
1 \/ v; r: b* s8 pstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |