下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
& }% k6 l, k0 g2 l. s+ B- C; qhttp://www.itwis.com/html/net/c/20100506/8234.html! G0 y0 Z+ z# G( @
! T6 f' C* \; `2 U6 e
程序代码
% @+ x0 _1 u& {- Q! U9 R2 |- E1) public string[] Split(params char[] separator)* O' M$ d( m# t @/ O& Y5 {
2) public string[] Split(char[] separator, int count)
2 Z, @; N8 k1 R+ u( Y5 D7 F" [3) public string[] Split(char[] separator, StringSplitOptions options)! L) v8 {3 {/ M7 z+ z1 I' b
4) public string[] Split(string[] separator, StringSplitOptions options)8 E" F' q6 i k- d
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
; C: J7 F% p2 M& d6) public string[] Split(string[] separator, int count, StringSplitOptions options)* C4 p- [6 E, W @
4 p- m; D4 }1 M% n$ P3 G/ O* y下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):% p- e) t" G; l& G5 L
' f) V$ v/ G# K3 t6 D7 g1. public string[] Split(params char[] separator)! M7 R$ X' Q* G- a0 U; B
$ r# o) G. O w
程序代码
: I9 [$ U7 Z0 U9 @% kstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
& b8 z( |3 z8 S6 e4 F! p1 Nstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}9 G3 D) v, L3 a' f7 i: }1 y
* S0 a y9 `) ~: b8 Z2. public string[] Split(char[] separator, int count)$ x1 P+ a4 C" q0 k6 t/ R
* ~9 }! Y& H# D( @ W: `5 p
程序代码0 A, W7 F# n5 D* d% n: }/ B
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}- a* H/ F, u/ W. M0 G! Z g% D
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
: n! A1 q( B6 k$ Y0 W
$ B% L( _1 L: o" ?6 ?8 K I e* l3. public string[] Split(char[] separator, StringSplitOptions options)
) `0 V8 m8 i0 |+ a2 G/ c
+ Q1 Q/ G e! m6 g0 q s 程序代码
, o4 f: D& S2 k. ?5 m# hstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素, u( c5 t8 M4 m3 E' t; N' P
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
! L4 j) ~# ~& L, m$ D0 @; G7 P) t' m3 s
4. public string[] Split(string[] separator, StringSplitOptions options)
# K2 b8 @& z* z7 q- b1 v; E% _: S& ]
程序代码/ R% I& ^2 V% {" C: [% J
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
S$ J, }0 Q5 V% R$ Gstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素1 ?: H8 H) |# ~+ _2 j
! Z# ?; r+ w4 Y. E% A; \5. public string[] Split(char[] separator, int count, StringSplitOptions options)/ }8 F; v7 N' R! y5 K+ q1 n
" W, j' S% D7 B$ u$ e 程序代码
( a, b& ]4 r- C7 @1 L5 @: W9 f$ Qstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素! @, `* K9 Q$ U( J' ~) M# v
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! r; k5 B( ?6 C5 O
( o. m. U* z% S# B, {6. public string[] Split(string[] separator, int count, StringSplitOptions options)/ y( x# L7 A9 B* J+ k
! L* s: K7 c' x, G8 M代码% J' A7 r6 q# u* T! @- q
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
) P/ T4 N4 N, ^& ` c0 B8 Ustring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |