下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
8 G7 ~5 |0 ]( Mhttp://www.itwis.com/html/net/c/20100506/8234.html
+ [' u* E0 N4 t2 e a# L! A* G2 x$ V; K3 o1 ~) ?8 h
程序代码4 \- k6 F. w9 H9 A8 J' L. b
1) public string[] Split(params char[] separator)
" E! z) \/ o" {8 x3 w% }* f2) public string[] Split(char[] separator, int count)
4 Z0 [$ _ Y6 y) `- v% C3) public string[] Split(char[] separator, StringSplitOptions options)
5 G+ h' Q5 j; z; T- m" i- a" ~4) public string[] Split(string[] separator, StringSplitOptions options)6 P% `; T: F" c3 K6 V/ n
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
9 F7 ~( j# D3 u$ r; C+ S/ U6) public string[] Split(string[] separator, int count, StringSplitOptions options)
" e- N) ^. R( w0 d& Z, ?/ o0 m
0 G" D$ z3 i4 k, n( w下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
! @& N n+ E) F0 ], A4 [ P3 {. O
# L* J5 ^9 h, ~2 U R0 v9 s1. public string[] Split(params char[] separator)# f, {( Z0 E( I3 O) K" P( ~3 V! {
; C# R, G. @2 o 程序代码5 Y9 L; |5 W# }- [" L
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}/ c, m9 `0 @9 A' G7 y0 Q- m
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
# g, U1 ^4 n+ E5 S% p" ^' D3 _" I% k( Z! `7 Q
2. public string[] Split(char[] separator, int count)/ b8 Y! G$ P, j* t( S# a3 }& f
R0 L* C+ Q& P5 ^/ M# g" o 程序代码
9 g; u& J' P2 h' k/ ?" d3 U% |string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
6 h* w" Z+ Y* W9 {string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
: {7 x7 e2 o: W% \$ J2 y: A5 T+ U1 g9 `, |
3. public string[] Split(char[] separator, StringSplitOptions options)
3 g; F, ]4 g0 N9 E& w4 g; P" N. {+ v1 A) d0 g4 J& D) }% L
程序代码
9 K5 {: H4 J" `# X* dstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素+ G, S0 P4 |- [8 N
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 F! f% S' n& F: o+ |6 a$ Z& k! U' ? B4 Y6 V- |8 ~
4. public string[] Split(string[] separator, StringSplitOptions options)
9 I- z3 M' w0 R' P- U, ~
( Z2 v/ Z; M" p5 w9 P 程序代码
9 E5 X" P2 {8 o$ H! ostring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素5 M5 d4 ~- Y' Y- [8 V% G+ b0 ?
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
[2 S- u _) `! z
0 Y' p/ p7 ~% B) E. O5. public string[] Split(char[] separator, int count, StringSplitOptions options)9 `$ q+ `, _! v
d0 m N+ k8 q; ]. ]" a1 }0 q 程序代码
$ E2 d& B! w; q" m) ?string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# X$ k0 T; |" f* |3 ?string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' B! f$ ~. |8 |) f1 j! J$ t8 w& |/ t9 v8 z
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
- k* E+ p2 R. [8 v. G/ q* Q
# k2 b! H5 m+ q4 z# ~$ O代码, X1 w1 r( F2 r! r* n1 u0 z+ _) N2 A
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素* D) C& c: _7 O& j1 Z. K
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |