下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
/ R. k6 l$ {1 |% w& F/ Yhttp://www.itwis.com/html/net/c/20100506/8234.html o ]/ G# z( g: Y; Y$ J
) ~: [5 j8 W$ ?! r& {3 |3 a程序代码
; F2 t% E) R, z/ T1) public string[] Split(params char[] separator)1 c# s2 u' e2 k3 C H8 ^
2) public string[] Split(char[] separator, int count), X$ v7 e5 F" h8 k5 z' V9 M
3) public string[] Split(char[] separator, StringSplitOptions options)9 A9 o% j3 t8 r, L4 s3 b
4) public string[] Split(string[] separator, StringSplitOptions options)( @" E3 S2 I5 p1 T* m ]& y
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
3 d) s: S2 x d8 }6) public string[] Split(string[] separator, int count, StringSplitOptions options)
& S! s; K+ @6 W- p, C& E" F, w$ N3 c8 I/ f/ Y! @& e
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
# Q/ b& r3 {8 i. ~( q5 x: |9 n7 R- v+ d/ n6 ]
1. public string[] Split(params char[] separator)
, L, |% p: Z; v5 Y) O% E) y/ {# ]( C2 h* d1 X8 @! n- [
程序代码: Z& Z0 l! z; h6 D- u
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}% F; d3 @ z9 T0 G8 s1 c- ~
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
: h" z" k0 y; Y+ D/ B4 \) H: T$ P' N+ z$ V$ B0 K0 r" Z
2. public string[] Split(char[] separator, int count)
- W4 e9 [) |7 J( `2 d
& D2 o$ x# H: [% A s* j) _) c 程序代码
! }, c7 B! D# e) wstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"} J3 B0 W. ~' k3 b9 J9 l+ _
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
# y! B3 U3 [# W$ I/ [1 Z& { U$ \! R$ T/ m4 ~
3. public string[] Split(char[] separator, StringSplitOptions options)
& T0 i n/ b: A6 J
$ _9 `" T; p% V 程序代码
* }3 |5 X* v8 s9 w) i A) ~5 ^% B& ~string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
& U+ ^# u/ x2 W) Kstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* E) ^! ?& _, m3 O- N' t/ |, L k8 ?% u6 J u
4. public string[] Split(string[] separator, StringSplitOptions options)
" n9 O4 j# P( Z6 _/ G ?
$ ^$ O2 F/ E% Q0 [ 程序代码
" G& Z! V2 d. Y4 [% o! cstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素! ^8 `! A) I y
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ @; X* `7 V) Q7 J2 a' p3 e% G
$ e& z4 g# Y, }4 A* M1 i
5. public string[] Split(char[] separator, int count, StringSplitOptions options)% u; J: s8 `. T; |) O& P+ }/ Q
2 i; i- R5 J. o5 f+ g7 d! M 程序代码5 E4 Z) @" i$ w A9 e
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
+ y2 Q6 h0 X2 p$ v- \string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素+ p3 s: S% c- W" c0 ^# K/ I. [
7 Q) J1 B9 E& c3 t( o
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
: m5 P( u/ j* }+ A$ H; X( h2 H0 A. f6 P6 B+ S. T
代码
$ |' @5 Z" p* |. r+ Kstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
2 b4 o3 H, f4 ^8 m2 @$ q5 d5 y, zstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |