下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
( `/ \7 P$ E6 M+ z+ bhttp://www.itwis.com/html/net/c/20100506/8234.html6 V1 w. z, ^6 O- K
# @8 R% u% k. _& k. V5 H8 Z
程序代码
; A- Y+ e, E" \9 l1) public string[] Split(params char[] separator)
; q7 z5 d$ O7 R; ~# h7 Z$ f& m2) public string[] Split(char[] separator, int count)! J8 M; k- w" ]2 _2 J2 y3 _ ?
3) public string[] Split(char[] separator, StringSplitOptions options)
% } K% s! I. X2 g! q; B0 {4) public string[] Split(string[] separator, StringSplitOptions options)
6 t" h B7 ]7 g5) public string[] Split(char[] separator, int count, StringSplitOptions options)
8 r0 P7 j+ t# t, k7 ^5 l6) public string[] Split(string[] separator, int count, StringSplitOptions options)1 m$ ?: x1 d$ T+ z& _
. g* O$ ^: q3 d, n
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
$ x% P7 {8 g) H/ B, I5 M4 m8 z# j- |* q; m- W
1. public string[] Split(params char[] separator)
, i- ]% n( P% _8 g" W" V3 B) x) l/ V) p
程序代码
- ^) f( \& D4 E) vstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}0 l7 r7 a! U3 g+ A* E+ c. C, V6 z
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
- Z& `! x: k$ T' l. {6 b3 m R% u0 p. F* p% b. e
2. public string[] Split(char[] separator, int count)& e: D; U; n4 }2 a: M
0 d; F$ C4 a, c' d( R2 K 程序代码7 C- n& ^# T2 z2 W2 W- N% Z2 W
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}. R& Q5 {$ U8 J9 t$ X3 y3 F3 E
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}# }0 }. q6 i0 I0 v1 A
3 l( O! }, L3 Q% [& @3. public string[] Split(char[] separator, StringSplitOptions options)1 C4 H: r5 c" l7 b% h% Q
% w) n+ q X6 P- n3 b
程序代码, v! X" g: R6 q
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
' u1 T6 D/ D: I! r; N+ estring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素' P$ z" b; f2 |% ?4 _4 x- v4 a
0 o E4 j% ~; s6 |! L
4. public string[] Split(string[] separator, StringSplitOptions options)! ~3 D/ c* e8 X
6 B5 n4 k! P, Y0 Z& _- I
程序代码6 E$ ?4 E# V7 U- a" H: h' |8 S$ ?
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素- L E% o3 b, a* U! \7 p8 l4 }
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- R5 E8 {1 @: d( h
) \, s( F* @+ J' Z1 z) p
5. public string[] Split(char[] separator, int count, StringSplitOptions options): X" B9 D# M K3 o! Z
8 K) j6 {, o0 N# J, g" U
程序代码# O- n9 B! |& M
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素* y% m4 r$ M% O+ l
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( D% a0 ^9 T# w
8 ~& N; e& N! `, T& ^8 _1 D5 ~; ]7 a6. public string[] Split(string[] separator, int count, StringSplitOptions options)
' `9 z9 |9 O2 G' B, T, i9 V; P0 t/ E1 f
代码5 n. V& F9 x: q8 I3 g8 M' C$ X
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素. o5 m2 Z9 m; D* @( r, `) w
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |