下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看0 A# Z1 d( W. X+ L8 \
http://www.itwis.com/html/net/c/20100506/8234.html
: s! }/ p$ D4 j6 c' ?
4 X3 M6 n8 _7 \7 K. o5 B" [& L; }' y6 G程序代码
9 [! @! t9 ~, ^- a# t( ?1) public string[] Split(params char[] separator)# C6 L% E( U$ t Y* |/ H3 [
2) public string[] Split(char[] separator, int count)0 r% n m9 j& |
3) public string[] Split(char[] separator, StringSplitOptions options)! t8 T- d" s! {3 Y+ l
4) public string[] Split(string[] separator, StringSplitOptions options)1 ~; u; }( G( j7 E
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
3 g! C4 D9 y2 l& N9 I3 w2 V8 R6) public string[] Split(string[] separator, int count, StringSplitOptions options)
$ r( K6 E) S7 g- m5 l1 o" P. f3 B; V6 \: w% v
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):/ L3 L) [) F. d% B; o' b& B) @) z. U
; G- b& n- x. ^: _* A6 D3 Q" M
1. public string[] Split(params char[] separator) ^* |7 d6 E2 L. Z& j
: L: H! b6 f8 H$ q9 w& x 程序代码+ B# j2 l, a# ~; L; x) o# e! D% {2 V
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
5 s; V2 G! e* l3 rstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}% a2 _* l( _" T: Y% {4 @: M) ~
$ C9 g4 N5 A- m+ O ~0 `6 z8 k2. public string[] Split(char[] separator, int count)
3 B$ e7 z% M, m* K2 f5 P ~0 J' u5 H& [) W
程序代码! m: g# M5 I2 h% t% ~
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}9 X3 K/ o( a* z- h/ {
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}! X9 r8 p8 }, G. T! c
# c( Q# i! u0 P6 ?0 X, a3. public string[] Split(char[] separator, StringSplitOptions options)4 P! H* D( g3 ?! z& O
: D0 ?0 ^+ K: ` 程序代码( D1 G9 H! y& v! k) S' [
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
' u/ d1 b" y7 \; Z4 X$ Jstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素: I/ P! d* o8 H; }' N# H# X% f
7 Q3 M# u* a, J$ C4. public string[] Split(string[] separator, StringSplitOptions options)
% J/ m, [- r7 `, M' N9 N) X5 v. ~ S1 d! [- Q
程序代码
) i2 \ w. o: h; H" ]string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素3 _4 E5 ^1 M# z
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
8 F `% w) ?' h( f. D* R9 K& q9 o6 [
& }6 B3 B, O/ `: k5. public string[] Split(char[] separator, int count, StringSplitOptions options)
\' z; z) Z4 B* k" Y2 I8 H9 W: c) a2 V6 c
程序代码5 u6 B/ j+ D9 Z+ N6 R
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素" n3 J/ H# K6 ]4 l# E
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% p" w9 o i- d, ~- [6 N5 e' h
5 m- h) M7 C. v7 T2 \6. public string[] Split(string[] separator, int count, StringSplitOptions options)+ n6 _4 ~ h. w6 M- ]& j
2 Q! _, K$ Y+ f w, O& v% q
代码
9 l. n( P: L: n& a Kstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
4 |, _% p# c; y1 mstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |