下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
6 ]& ~, \+ i# u% I' Vhttp://www.itwis.com/html/net/c/20100506/8234.html
1 p4 j- l! K4 d6 X6 J t/ c) V( o8 [9 ?# w: S% }
程序代码! V- d, W7 v7 }9 ^# y
1) public string[] Split(params char[] separator)
7 ~1 ? x: I; t+ p2) public string[] Split(char[] separator, int count)- j+ c1 b2 t# i( f4 Q
3) public string[] Split(char[] separator, StringSplitOptions options)
f5 |% l4 [% G" o) x; a4) public string[] Split(string[] separator, StringSplitOptions options) O. Z( i& o [2 B9 R& C/ H
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
2 z" K+ C O& K+ q1 N% M% [6) public string[] Split(string[] separator, int count, StringSplitOptions options)
3 s' u# f3 U4 o( y/ W) _
1 @ F; l" ]# u6 _ M" w7 ?# ]$ }下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
I+ m5 r, ?& f; g: D* ^
: d; H! z. q) |; H$ k0 Z, p: E1. public string[] Split(params char[] separator)
4 ?7 E3 b& k$ x8 D/ A2 s8 O) [
4 j3 F1 S, x+ ]) L% @3 G 程序代码5 g0 B: _ E2 \8 r. r5 _3 a( b% L
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}0 S( q. J/ R5 g/ B
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
3 [+ \$ x+ z# m( Q. `' \! t
- ~0 Q- v. r9 Y0 n- z2. public string[] Split(char[] separator, int count)2 e/ t9 ~- E G$ D/ a
$ Y7 Z: `% }" T 程序代码 ^! x) h+ t& H6 B0 m' Y
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}& B, I6 ^% I b# Q. g7 F& f
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}* {3 O4 f1 G6 b+ N z( y- k* }
1 r$ L& S) ^$ ]9 p- a9 o% `3. public string[] Split(char[] separator, StringSplitOptions options)
5 C) r* {/ `9 q7 ?
; h" D+ Z# C! x. b3 G6 [5 G! L& q& u 程序代码
7 @: j9 b6 a' t4 U0 c9 @ hstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* Z A5 x. p4 k0 a/ M7 k
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* h2 n( ~' Q+ B* V5 j. _# Y! U2 e: j7 G" I! z+ O1 M* f* W
4. public string[] Split(string[] separator, StringSplitOptions options)
1 R8 {5 Y0 R) l# a* f! P$ f0 ]; ~# U
程序代码
! N9 n( q8 u) a4 U7 k; h1 q; tstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
* N( j4 \ }: M( ^4 Fstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
- T( b: L0 X5 s! x I B5 y8 G! x P8 {8 Z1 N- v) h/ m# A! Y: k
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
3 U$ @9 a3 }) ~) D( m+ p2 t
' r: L2 w5 ]7 _) w: Z0 F 程序代码) T @$ F( e$ c- K
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
8 D- d& B7 h% Q* w6 [+ Q" v8 v5 xstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素4 J. W$ ?5 [/ \+ q [
* ^" V9 y9 E# P/ M3 X
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
% F8 B: c* D% @) q. H# T( p: v4 M* G, r X, ~* g8 W
代码
% ~( ~8 I& u9 ~/ q1 |6 K2 q! e: Istring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
! O+ c$ O: ~! R" Ostring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |