下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看4 B" i& E8 d" M
http://www.itwis.com/html/net/c/20100506/8234.html/ l/ R; x* |) S3 o2 O" A5 h2 [1 L
9 S! c8 l- ~2 i8 T) C( ^2 a- U# z
程序代码
2 W2 k" z+ J- \- b" Q' k1) public string[] Split(params char[] separator)
/ v9 v9 _ S3 x( c. ^6 [2) public string[] Split(char[] separator, int count)4 K* Q, e8 P. p5 L: T, y0 I
3) public string[] Split(char[] separator, StringSplitOptions options)
( U! X1 x( c; J2 Q4 L4) public string[] Split(string[] separator, StringSplitOptions options)+ Y, m# i3 ?2 w+ s/ ?
5) public string[] Split(char[] separator, int count, StringSplitOptions options): _% c: h+ `* j: m6 D
6) public string[] Split(string[] separator, int count, StringSplitOptions options) k; j1 M0 c' E- P; v
+ P4 g. X% k" Y& y8 D) s
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):3 Q" p Q& f; M' A% H! g
5 S# W0 a$ g" J1 x" Y1. public string[] Split(params char[] separator)
N8 Z# X' O1 E: L# ]0 l3 H' O/ M/ _4 \% p1 f
程序代码8 h) y: c# d% u% F* ~% m
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}; n( x+ d7 F! F2 _" A
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
% E( N" {* ?# I6 B( K; k- G0 S; |; }. |; r
2. public string[] Split(char[] separator, int count)
! a% C. \/ b# m9 v# `( y( R
/ r3 g9 m4 ?# T s! j 程序代码. A5 T2 R$ P7 F/ [' k4 }0 b
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
# r0 `$ n; T# R( V- Estring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
) x3 A. Q* ~; P8 ]# s
) L( ?9 Y4 g; o7 O5 {/ Q, R+ l. [3. public string[] Split(char[] separator, StringSplitOptions options)/ b' s9 G: l0 d5 T
% _6 M5 R: J) s Q9 n
程序代码/ Z. q# j8 A6 y0 l H* ~/ u4 y# ]
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素3 i6 Z) [4 q1 \( [
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
G4 p* G1 \& S6 ~$ w# J' o0 T' d$ W) N8 R; P
4. public string[] Split(string[] separator, StringSplitOptions options)
. Z% w f- t& X" \
& b4 V' w1 X8 I2 C0 I 程序代码
& q* ?' z- O+ {: u0 Q( Astring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
6 l, U, J/ x$ O, H+ gstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 a2 y, x# Q" B1 n; a8 A# }
2 f0 n* m; O% a* Y5. public string[] Split(char[] separator, int count, StringSplitOptions options)* H3 I- y4 D6 B2 P
) z$ e' Y& u: m7 D0 E
程序代码. X" ~0 D0 b. X! A) S# ~8 Y9 ~* O
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
+ W3 I1 Y: T, {. ^* n3 e' Jstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 f% W, w. ~2 `
" e6 r( M, `8 M6. public string[] Split(string[] separator, int count, StringSplitOptions options)
2 z; A. n1 S) D. z, u3 E' n
; b6 Z! U# A6 _0 {代码
5 o9 u& t# x8 R7 S2 D% \) `string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
" F6 w4 \$ `( Z% s astring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |