下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看& T( e& O5 ~, n9 ]7 w- _1 a2 y
http://www.itwis.com/html/net/c/20100506/8234.html9 T1 l) l/ Y4 V% k1 B; F
& h) [! z( A* {( B
程序代码) B; M- x" y6 [ b/ t' n* L
1) public string[] Split(params char[] separator)$ o" B' x" `# Z6 E5 Q5 U
2) public string[] Split(char[] separator, int count)/ K6 _" Z9 g, Q% i& A3 G
3) public string[] Split(char[] separator, StringSplitOptions options)
; k# k. _- ^7 |. Q4) public string[] Split(string[] separator, StringSplitOptions options)
& H" k9 E3 L, [5 X* O" x& I5) public string[] Split(char[] separator, int count, StringSplitOptions options)9 v6 _1 w8 @+ i) b0 t
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
8 `, i! ^; K. h8 {4 P5 o' g) n. c. }$ a; c+ K. C% `
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* ~& b$ Y! z# g
! c/ a4 F; R6 p( V3 E3 X5 q# s
1. public string[] Split(params char[] separator)
1 `. L3 l) L1 ?$ G' I' F; K, u( n- ~* j+ N
程序代码: h( e2 d( s( g! l& W% A4 Q
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}* z+ Q5 X5 t- ^- Q* G1 t) ?
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}+ Y3 W1 J x; ~
" N- `% d7 I$ ~& b2. public string[] Split(char[] separator, int count)
6 @' |3 \( [+ h$ b. c5 w; l2 A$ I7 q- i9 D. L, l
程序代码7 o1 I$ [$ d* |8 t
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
& Y2 D4 U# g: E" n) e' J% q$ [string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}: ` [$ c0 T0 l: F
' ~# z) f1 B% U( O% K+ c3. public string[] Split(char[] separator, StringSplitOptions options)9 B2 ]0 F5 j" w" _6 ?5 o1 G& l' e: ^
$ }2 W0 F4 v) K' d6 I
程序代码. b4 p2 p( ?: m- [$ j! _
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素, I& Q2 u" N$ u1 T- @* b2 `
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
# C- J3 I$ Z7 I+ w9 p8 z
& F* N: c3 S9 ]6 `! p/ e* @" y4. public string[] Split(string[] separator, StringSplitOptions options)
) H! K" H& [2 B" {$ A7 y1 o7 a/ @# \3 H( a7 [) A
程序代码
1 P+ K7 E% s/ {" Q. e; ~. ]+ ^string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素2 I& U. k6 ]! N* a; T$ v1 v: ] B
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
9 a" L9 R2 z0 k3 Z6 O3 i
) l0 J0 ` d" ~7 D5 a5. public string[] Split(char[] separator, int count, StringSplitOptions options)
4 r& p" U3 a2 ~. \. H( N- R0 ~1 `# ^ |( t; \* K) P
程序代码
3 d. s& D/ \( H+ x6 n' Lstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素5 a, s( i8 u/ d6 `
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素4 V1 r; t9 P r0 H
2 Z4 v: i9 R O: J6 K6. public string[] Split(string[] separator, int count, StringSplitOptions options)( D" F+ F7 C) { |4 i [
8 a0 X& n" H: x2 _* I' C
代码8 _% k, c- W) W% u) e1 ?
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素5 S. t/ O) J) j
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |