下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看$ z' L) O3 E- R$ b O/ A
http://www.itwis.com/html/net/c/20100506/8234.html6 D+ w% y5 R3 Z' G- E' h
; S [ I( b2 P
程序代码( T0 P4 a& d/ Y! U
1) public string[] Split(params char[] separator)6 ?, j- M; b( _9 a' {: w3 L/ `
2) public string[] Split(char[] separator, int count) A/ \) I3 a' x
3) public string[] Split(char[] separator, StringSplitOptions options)6 w% w7 g! D1 g: c
4) public string[] Split(string[] separator, StringSplitOptions options)
- P0 d% ]" J9 H/ c5) public string[] Split(char[] separator, int count, StringSplitOptions options) @$ [* h8 ?- [& J( V; L
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
) V3 W- F- a% j1 z/ b1 A9 y6 C! Q- M$ i5 b% k
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):0 t6 \$ _4 g. |' X* i1 V& i! ]& ~) ]
0 F8 ?& x/ p2 u3 h3 r
1. public string[] Split(params char[] separator)
% V, h: \8 J) Z' j+ R- c0 V+ m5 G8 o2 w' R0 f
程序代码
* U* @4 N. a; K% K# v1 b" |1 p& W# bstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
$ ^5 s! G( y1 T3 [9 W m0 u: D6 Zstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
9 O+ u$ \. @3 A/ `; b/ W& h/ v0 P1 h1 }/ V0 d) Q
2. public string[] Split(char[] separator, int count) Y* I+ t8 K% g0 v1 G
- S4 N: e: X6 i L5 Q
程序代码3 b) ]; C" F9 k' N4 |+ d. a! \
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
5 Z+ r7 S T# r; o) Kstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
; g) H& [! r4 G' {+ ^. c4 A% L# R& t0 [( U3 J
3. public string[] Split(char[] separator, StringSplitOptions options)( v* f; l( c) n. H* E9 b
( r+ | Z: X" c( D7 r
程序代码1 |# r9 W W( h( D4 [; w* @5 h' u
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素2 L6 k4 e8 A6 S3 a+ y& M6 O
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素3 q( v* L8 J5 [, M2 w% x0 @5 q6 D
' i% H$ Y. l; i- e$ b4. public string[] Split(string[] separator, StringSplitOptions options)
$ |$ ?0 L# P, H$ i0 d: {& {. ~/ C) d d. Z9 C7 c$ j
程序代码
' K1 j6 d$ I V* ?) bstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素. ?* Z5 I, D: E; Z" {% r/ Z' r& k
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素& Q) m5 i- a( Y, r. h- i
" B' D5 {$ v# g5. public string[] Split(char[] separator, int count, StringSplitOptions options)) `/ E( x7 A) l$ p$ C
3 P. |# s/ w" ^1 L% H7 B3 o* ?
程序代码9 {9 k+ \( C; b, h
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
: R o3 k2 I# xstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
8 @5 y1 v6 u3 f0 }' M
% |( ?' m* O% B2 j5 ]6. public string[] Split(string[] separator, int count, StringSplitOptions options) F- i) L+ s6 D- R4 m
! c" g6 B2 k, M ]+ f* _代码6 f3 s; M9 o7 x
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
: M+ V+ L" b/ j4 g6 z! Qstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |