下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
' r1 ~' n) A' Z0 Y# q" Whttp://www.itwis.com/html/net/c/20100506/8234.html, O l1 H- S5 ?4 V# {+ `
; f' Q* o+ x' c# ^
程序代码
6 N- A Q# N& Y3 [. \1) public string[] Split(params char[] separator)
% L8 F# t2 R% q/ E" t' z2) public string[] Split(char[] separator, int count); ?% ]$ p5 s L( V+ B& u+ O
3) public string[] Split(char[] separator, StringSplitOptions options)% t- B( H: F5 w) y+ ]+ y7 d* V! [
4) public string[] Split(string[] separator, StringSplitOptions options)1 O" n% C* M; O7 V
5) public string[] Split(char[] separator, int count, StringSplitOptions options)% u T# b% B. p! c% `! [
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
Z o1 q! u7 V5 H% e! n/ j
2 Z1 w# e- E9 z9 e) S下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):8 |0 ^+ m/ `6 g
/ E. _( n) x$ @ c6 Z& `) q7 I1 [1. public string[] Split(params char[] separator)3 C% J" z% x, y/ o
! p0 |! U; ?0 m5 X c b; \1 V" ]
程序代码8 W0 e! N N: J8 C# g' U, x3 l
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
4 u2 F9 y* G( w5 d ostring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
% {$ [% u9 L: V, A* Q" e+ T8 x% T |: ~ {4 G. I! a
2. public string[] Split(char[] separator, int count)
8 L1 t$ m2 c1 Z8 a6 A1 D$ D
+ \! Y4 H! }* e" ~; \( z6 Y 程序代码
o7 k& H( M4 K" Pstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
% z! `/ r/ j3 I: t- B& k. M& astring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
5 |! T9 b1 f" e7 G
+ G* g$ ^& L: w A. p2 d# A3. public string[] Split(char[] separator, StringSplitOptions options)7 n2 B o9 l9 I( O7 \; o. W
- D4 Q& O' y% z8 K0 ? Q 程序代码$ ]" B7 W( g7 C! `
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
9 x3 E# P" s0 [( J' Q! G7 _; istring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- Q" o$ i/ c# N. }' C
4 C. }4 f8 o7 T4. public string[] Split(string[] separator, StringSplitOptions options)4 |* G& U6 A D
' {; L. D q' E7 K5 ^8 e7 s) y9 R 程序代码
' a: x8 x- G/ H, N; Fstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素, ?5 c0 z, m7 ?0 N, Q' p
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 d* n3 Q4 N+ A/ I, C) r% m) F, i [4 I5 o+ i. P& y: U- W
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
7 k' W( B8 S/ ~0 u& H( b- F) n. V% A5 n% t' ?+ z
程序代码
S7 h& y" M5 d5 }2 b9 X& @9 Dstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
: P; j/ F/ R) @2 R+ K; A9 Kstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' l/ \! R- A1 B( l/ C7 L4 ]
, v/ L3 U% B9 M0 h2 ?; n6. public string[] Split(string[] separator, int count, StringSplitOptions options)
# X* `# Y* s: e* d" w5 u# R% _9 \& _7 Z
代码
9 n2 F: u b6 Dstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
1 `; Y% J7 h; Ostring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |