下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看. C8 P, Y8 l$ E, {3 o, X2 _( a
http://www.itwis.com/html/net/c/20100506/8234.html
8 Q0 ^, A3 c4 i
/ ` T; d, t1 N* M# ?程序代码
$ V+ g- [; |: L9 C5 f* `% P1) public string[] Split(params char[] separator)
/ m7 h2 P5 U \/ l8 c8 z2) public string[] Split(char[] separator, int count)
- S- y- D; q0 Y- q: \9 w% @4 j7 g3) public string[] Split(char[] separator, StringSplitOptions options)
F9 n6 N# R7 l5 ]+ [0 t! t# }4) public string[] Split(string[] separator, StringSplitOptions options)/ p0 L( L! C! ~/ X
5) public string[] Split(char[] separator, int count, StringSplitOptions options)* U* I# }# O$ H) f( H+ s
6) public string[] Split(string[] separator, int count, StringSplitOptions options)$ M7 k5 U9 h6 Y a" Q3 R; |
) Y% u2 N3 b# q$ r9 x) @$ Z9 P下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):. `" R; c% X# K+ g0 f. z# D
4 e8 @- ^# x+ y2 H3 h. @6 v$ q
1. public string[] Split(params char[] separator)
) a; U* l: p+ I% `# N1 I/ K I2 G" K
程序代码- r0 [4 [" \1 Z; @7 V
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}: Y) @6 `' N3 P0 P, b0 D5 P( c
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
$ X* w2 F9 Q0 @1 N& A6 T w5 X3 x S# |
2. public string[] Split(char[] separator, int count)* ]8 M2 q3 [; [4 ]& q+ y/ D
' ~3 ~$ Y1 p. u0 c 程序代码
. \4 B, m* W" y' W2 z1 L7 a8 fstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}" K. J# S- B, I5 U( Q3 y* a
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
0 \: l# s, o, Y( O$ r2 C1 P- O6 Q& S) m
3. public string[] Split(char[] separator, StringSplitOptions options)& k% S# u+ u5 Y* a1 ?: b2 t8 F
1 C, U8 o. n/ Z; M" ~0 W 程序代码% W' a' \; k) H
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
" i) F! y! Y% L: vstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( d$ [: F% E: U+ J& |2 g3 v @. H' d
4. public string[] Split(string[] separator, StringSplitOptions options)
( U- I) S* `: |$ \5 h! a& o u
0 h: ]- q* X" c$ S2 L 程序代码
7 c: N- B, k+ W2 e& ~+ W8 tstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
; n. u6 \& W5 _3 x- Z8 d Gstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
- B( B: p$ E+ Q# o6 f" X
- p7 u. K9 ^' `+ W9 u5. public string[] Split(char[] separator, int count, StringSplitOptions options)0 H9 L( z: r7 m# V% E
! F6 }0 a4 d: n) y8 w3 C/ y
程序代码
. C- { ^# I0 ~; `, \2 ystring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素: \0 h% f" ?6 j
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素. R7 U1 G" j" B, E
* ^% }0 i1 N) Z% D+ |: c
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
. e8 \6 B# b* n+ y1 t, C) [# I# j7 w3 B: l" R7 Q7 i
代码8 [6 a6 w. n: I# R8 y; j
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素- H# q( w; O. |: H7 z
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |