下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
5 G4 q, {* q/ y5 z. Dhttp://www.itwis.com/html/net/c/20100506/8234.html
2 f5 A; C7 _; P" r& d1 v
+ m2 t2 p6 E( P( k% H程序代码+ I- p5 V+ z0 J
1) public string[] Split(params char[] separator)/ V" n! \5 |4 F9 D0 s
2) public string[] Split(char[] separator, int count), f6 U& X% l' B) |+ d9 k
3) public string[] Split(char[] separator, StringSplitOptions options): S, N6 Y4 I: R: _7 l
4) public string[] Split(string[] separator, StringSplitOptions options)" m h/ Y" Y8 s, A @- @4 T C. _" T8 [
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
& A6 E+ o" H D3 s5 m6) public string[] Split(string[] separator, int count, StringSplitOptions options)
: t; @5 _* G5 Y: q$ b
_" \( z* Q# B! n下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):6 [( t& w2 Q) e) z% Q- \& J
) T) E2 a4 Y* k4 }1. public string[] Split(params char[] separator)
3 U' u& f+ T; Z. L6 A
' X; ^7 e7 S7 O5 n& ^! h" p9 z 程序代码0 {1 B0 Q$ z2 y, H" B5 o0 A. t$ u
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
1 g: C" @. f7 e, v2 lstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}! ?# E: l0 n6 E4 Q4 }# @7 r
; ]( `" d) z+ t) h
2. public string[] Split(char[] separator, int count)- x% h' w- {. R* L
" Y; h% i V, B( y7 S/ ?' t
程序代码6 h$ z2 i9 `; h4 w, Q7 {# ]0 `
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}/ _4 b; [* z4 Q' H4 P3 V. J
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
) _* k5 l! v9 N6 w' F4 e" i$ G4 N; r K8 M- \0 p
3. public string[] Split(char[] separator, StringSplitOptions options)" r5 B- b2 M+ Z, o
9 i+ b: h/ d" ~! W7 n 程序代码) x8 w9 a# u$ O, a) @
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
+ [/ f3 O! [ N+ l1 kstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, R; Z7 R0 H1 z: K% ?% s
8 @( T1 p0 |( U. Q6 z+ b4. public string[] Split(string[] separator, StringSplitOptions options)
3 C3 R |' G& V! c) @7 A: `# o% T, \3 q
程序代码! E# j& O6 i% V* u" E
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 n: [6 K6 | o# ~( b
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6 o2 I3 o+ v- w, H0 I9 E/ r) A( r! b# z; Z0 k
5. public string[] Split(char[] separator, int count, StringSplitOptions options); p: f' ~) v# |
& L, i, r# U. \$ P K( R1 S; q
程序代码
' _) ]7 g+ {2 Cstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
0 W1 c: L1 f* `) a$ ystring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* |" A9 s8 G$ _) b I/ N% A; n( A1 C: E7 p' a# @9 U5 j- ?9 B
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
. i1 A9 J& O) }$ U
; V: i N/ w! D9 K$ W9 Q代码
8 W. I$ T% J) U' v/ xstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素* t- z0 w# b# g5 M( l' A
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |