下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看' p4 t9 s5 L/ F3 a' x" t, b, k* Y
http://www.itwis.com/html/net/c/20100506/8234.html
: D: S+ n7 I8 v7 G8 W: h
, Y& C" `1 n) ~' K# N, p/ F3 o2 X程序代码
# C* `. P a/ D( @7 O6 o1) public string[] Split(params char[] separator)
1 W/ T' y" y& j1 F- u/ i" H2) public string[] Split(char[] separator, int count)& A# |8 J% W% V j6 x
3) public string[] Split(char[] separator, StringSplitOptions options)
$ f4 H+ K# M. R9 d* z: y' z- g4) public string[] Split(string[] separator, StringSplitOptions options)
: ]2 ?) L0 B' g0 v ]5) public string[] Split(char[] separator, int count, StringSplitOptions options)/ T- o. B% W# f' ?/ [
6) public string[] Split(string[] separator, int count, StringSplitOptions options): m3 \ b b* a6 M4 c9 r: r
1 I. {8 L" z& e$ C下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
5 J8 k" ]- z$ n0 `& ]0 W( r
. b- p) T) J% C2 w1. public string[] Split(params char[] separator)
. L) J. L6 ] Q) Z7 h3 N" F D! B& g# \+ [, j6 R, @7 W
程序代码
2 e( r2 W9 l/ i- L( ystring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
) Z1 `& E- L6 Gstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}" G& M- H* C3 `( y T8 j
3 Y3 l1 Q5 m9 e; d. Z' @" a
2. public string[] Split(char[] separator, int count)
5 N" V/ p5 z y) [ h. m+ q7 a* e% u- I8 m3 F* ^( `
程序代码
1 N1 ]& S( n: C' D( X7 \string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
8 p- Y/ @: J; s; @string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
) Z8 ?, q* ]( F7 ]6 ^7 ]' z' @' s, }( n( x$ C" _, Q4 c
3. public string[] Split(char[] separator, StringSplitOptions options)
+ p& t# C# A" i$ |% d p4 L. e
" b: A q# A1 N4 J 程序代码
/ L: W# Q, ?, x: {) Rstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# J; o9 y- v9 I3 ~% ~. C% z2 L3 n
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' {# @9 f) Y6 T/ u( B8 h, d. m1 u6 n H
4. public string[] Split(string[] separator, StringSplitOptions options)
; `( |7 Q" F0 ^8 K& M- J6 F/ A6 Y" G$ v6 D7 S, l
程序代码# P8 v2 V- l4 v/ S0 P
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* _( u2 C6 |) H
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
$ i8 N' k# x: |4 l% I$ w, o5 h) p: I o6 \9 g1 o+ A& y
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
1 b2 |, ]" e P+ j. @
( p+ n w3 o0 ]. M$ S 程序代码
% h5 |9 z- w- L! nstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
- ~" c- G! c8 w9 x$ D1 I+ zstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 T7 y' X7 d2 x5 k2 g% M0 U% U( L8 e( D8 k; g5 ~+ X4 G
6. public string[] Split(string[] separator, int count, StringSplitOptions options)0 h4 C% @4 ?& b3 b4 m8 e
+ \) F0 o$ J: w. T代码: i; D% `7 D4 B+ h5 H* x
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
: k1 W7 W* O, V. p1 dstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |