下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看0 ?+ A% W. p8 b2 E
http://www.itwis.com/html/net/c/20100506/8234.html$ ~! s0 Z" e, W+ Y+ F' K
+ \/ A! u: e. B9 m1 q5 q' H3 d
程序代码8 Q$ a% U3 j" e5 J) O3 y: C
1) public string[] Split(params char[] separator)
; Q# a3 V8 i; g9 g, W1 |6 W& a2) public string[] Split(char[] separator, int count)/ ]1 N' D5 U+ X
3) public string[] Split(char[] separator, StringSplitOptions options)8 ?: Y0 h6 f4 g
4) public string[] Split(string[] separator, StringSplitOptions options)
4 T) ^3 f7 e$ o1 T6 G, e5) public string[] Split(char[] separator, int count, StringSplitOptions options)
- c8 a7 H+ { B' l! D6) public string[] Split(string[] separator, int count, StringSplitOptions options)
* n Y+ q8 A% u: F* ?
4 I) k7 s& Q% u/ u下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):8 n. ?9 H4 B# s( R0 v; Z) q
+ L( S! S" P7 a/ G" Y
1. public string[] Split(params char[] separator)7 [1 E" I- m$ c8 ^/ f
* M: i/ M! b: r0 z5 v' ~+ @! v
程序代码
2 j1 |, b& Z2 Tstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}7 W6 z/ O) j+ n: I3 B T3 ?
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
9 l0 C/ J D+ {) O4 n. S
. f5 v6 M2 R$ D" I, x2 e) n Y2. public string[] Split(char[] separator, int count)
+ f8 ?! r, _( `6 N6 f) q" z# u% ~, t) {' h3 F$ d! ]8 k
程序代码8 _" D" ~9 @7 c
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}9 k* F; E' m7 @ Z6 f
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
9 ?+ x3 U! I8 Z: g F- ^; H6 M0 ?/ s( v3 d: j2 L" K2 p7 g& w" [# ]9 |
3. public string[] Split(char[] separator, StringSplitOptions options)
6 n/ f' j1 a" I3 q; v5 G% \1 o8 a; k9 S b `' |- s
程序代码: |; B5 ?% y7 M( F9 ~$ ^& ^0 j, m
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
E) i% ?& ~" I$ m1 istring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素5 V$ z( W7 ?0 n( a1 K
3 m2 w- M4 x! t" Z+ q4. public string[] Split(string[] separator, StringSplitOptions options)
! b- w3 o: j2 J! w' m }3 A( _! J9 E a1 B
程序代码; E# Y7 T7 I# v k8 F+ q
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素$ }4 w( W0 F1 v2 d( M
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素. c+ i9 L1 @* r0 w
1 y) E H) E7 x# j% U7 q5. public string[] Split(char[] separator, int count, StringSplitOptions options)
0 y* W" m/ z& |' h+ Y
% j! z5 d4 s) n1 p9 w 程序代码0 O/ m6 t h& U3 k' i/ Q
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素+ j+ i+ o4 }: l+ e: F- s8 E
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素) Q/ F* `# Z$ Z
1 b, @$ z8 \% c L* P$ z
6. public string[] Split(string[] separator, int count, StringSplitOptions options)* A9 Y# X: v5 _0 Q2 s9 w2 g
9 R4 |8 I3 c" g; |5 `! A6 ?
代码2 L) z5 X% m0 R7 `/ _' c2 s' i
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
( I N( n2 A# T9 @1 S) Mstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |