下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看3 t$ M/ J% B: j
http://www.itwis.com/html/net/c/20100506/8234.html
! y8 \9 H, \* I$ D7 D5 i4 A7 W6 E
' F: {( ^! _6 {程序代码
: W2 `( Z& E ^3 i) `, D1 v2 A% O1) public string[] Split(params char[] separator)
8 j' E/ s$ F `4 x2) public string[] Split(char[] separator, int count)4 t3 \+ w- R0 M
3) public string[] Split(char[] separator, StringSplitOptions options)
, P, ]1 o5 R, |- d: ~/ I0 |& h% G9 A4) public string[] Split(string[] separator, StringSplitOptions options)
* D9 N! Q9 ?9 l5) public string[] Split(char[] separator, int count, StringSplitOptions options)
& C# G' ^3 g$ X! r6) public string[] Split(string[] separator, int count, StringSplitOptions options)3 b. @% U. `: _
* [6 ?1 m7 D6 j3 W下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):, p6 P, y" i4 I/ x6 \
9 E2 u1 t" r) C/ t; b8 {. l& W0 i
1. public string[] Split(params char[] separator)- ]9 p1 c, I7 T' h9 _. m& ^
* a; F1 A% S U' X 程序代码0 W. A! _2 T- b/ b; R/ ?/ x
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
+ f: S: q% T: s1 p* H! U( nstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
Q* F+ v" B4 a5 B+ r
6 M+ ~+ I6 ^* ]+ Q6 m3 E3 S. L" A& j8 s2. public string[] Split(char[] separator, int count)
% S/ L* ]' V* T. k" i: R8 R! j6 M3 [. o ]" s1 _( t6 l
程序代码
9 x/ i/ Y% t% U+ zstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
" |0 ?. `/ w" e$ f ~5 b2 Xstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
* \! ?2 F9 w5 t% v6 K' A4 ?1 s
2 Z# C5 q1 w6 o; J) L" o3. public string[] Split(char[] separator, StringSplitOptions options)
, L9 h ?* e1 V, \# l: y7 d& a
$ Z2 J# o4 _( M. K 程序代码
# M6 k3 m2 y4 }6 cstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* m! ~& ?5 @7 N4 Z2 M8 v8 v" a
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
+ G! V0 o9 U3 j" i/ P4 a
: b) W8 T- S# J7 {# @! z; J4. public string[] Split(string[] separator, StringSplitOptions options)
9 w% A8 I5 V1 ^$ J* M% d6 p% ^
8 V) w- d J+ [0 N; ?3 j# y D 程序代码& x2 z. M" K6 P- k) j( U5 X5 x& V
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素: ~3 M3 v# r$ X/ ~& [) @9 {
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
4 V8 B# C: d, k3 f1 ^7 @" s# _: c
3 d4 e h }( x& U% Z) t4 I5. public string[] Split(char[] separator, int count, StringSplitOptions options)5 R; n! h v* o7 p9 E, _$ h
: g- [: L: W, F1 @/ a
程序代码
& _+ Q3 s( v- _. @! ?' cstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素$ a- A5 ]! x# ?! i
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
+ F9 T0 i+ E. O/ ^4 ?/ s9 S5 u* h+ D9 ]' }% S: X8 G7 r" z
6. public string[] Split(string[] separator, int count, StringSplitOptions options)% P" a/ l) y! T7 _) c
! T- h3 O4 E; Y- g3 P6 d3 y! e; [. ?代码# @$ V- S: m' A2 P! D/ ^
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
6 J c. e3 t3 [* k( istring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |