下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看( D6 W4 P( a( @; R9 |4 Q* n! @4 G3 T
http://www.itwis.com/html/net/c/20100506/8234.html5 h& n$ U" b6 Z" s2 _, o
0 m3 x* X: ^6 Z! c: B) W程序代码) B1 v8 G" O( `- f1 r1 i
1) public string[] Split(params char[] separator)
" D/ H# Q2 f+ ]4 M# R H% H& m2) public string[] Split(char[] separator, int count)
- D7 P, e$ x/ I1 D' t! @# m7 v3) public string[] Split(char[] separator, StringSplitOptions options)2 ^$ ?( [7 R4 H* M6 z) X
4) public string[] Split(string[] separator, StringSplitOptions options)
+ M7 B+ H' `* i, e& l5) public string[] Split(char[] separator, int count, StringSplitOptions options)! ^) b/ D; P9 [+ n2 Y8 j# p! l9 v3 q
6) public string[] Split(string[] separator, int count, StringSplitOptions options); S- P: z: b9 a( M8 T
& ^" H& H/ }8 \: a2 s
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):( l3 J) ?# I6 E
; g$ f0 @& Q% T4 c. a1. public string[] Split(params char[] separator)
$ Z# K4 l/ J; A" X" ^) J. F( q4 ]7 r& D$ W/ Z$ a
程序代码
: ]3 m8 k4 H7 n, ~string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}$ S1 H6 \( C, I( Z
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}& X/ b, k$ z+ f
9 i8 K9 F W) U3 S4 O0 W0 _* i9 I2. public string[] Split(char[] separator, int count); |% t0 d9 U; S i6 y. ?
" U, \. s# n9 x. d- z' ?
程序代码' ~7 c( r. I2 q9 _4 s @
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
5 A3 S! ]* s h. _& e' Z3 wstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}$ a1 x$ O$ O- X Z8 a1 Q$ x5 A" k
6 }" Q# j2 ]! m9 L5 E
3. public string[] Split(char[] separator, StringSplitOptions options)
; z0 g" x2 _* o% u+ b: j K1 ^) ~; U, U) @0 w
程序代码
0 L% u; V, d8 k% M% B2 ^9 a' {5 Wstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
! R% u" f& c, E( R3 astring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素8 j7 P; i) Z7 a/ S; C
8 U, r; P; X# f4 v: S) R9 N4 h
4. public string[] Split(string[] separator, StringSplitOptions options)* R* t5 h/ q; r
4 p( N3 Q& R8 k8 U2 U
程序代码/ R5 k# s7 B7 P: A* X4 L
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
& W) l5 M! u2 I! E" O3 ~( U9 {string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素1 W4 C2 D2 }0 G
* J+ t% _' p* v8 f5. public string[] Split(char[] separator, int count, StringSplitOptions options)9 o, U) l- k) U6 P
1 }1 _6 q2 }9 g$ s- t1 X
程序代码% q8 H* N/ o( _- @
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素7 `1 g3 j7 f0 C/ ?; C8 e8 ]+ o
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
! b# k: o1 D* c+ [ [1 P
4 K$ D/ w0 z+ g5 j: z6. public string[] Split(string[] separator, int count, StringSplitOptions options)+ R4 ~# A$ P% I/ I0 q
5 s, I/ l1 G7 c U8 M- M代码
5 Z# V+ a4 F! e# zstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素* L/ @+ F7 s, z1 H& }8 J- l
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |