下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看; M6 ]6 p- b5 U" | i
http://www.itwis.com/html/net/c/20100506/8234.html
2 t- H4 h$ G# l5 u5 F! H4 q" {* G2 L6 ~+ v
程序代码+ [: V1 [: ]4 i: V. \6 w1 x
1) public string[] Split(params char[] separator)
# h v5 ~& {( A' E H: Y2) public string[] Split(char[] separator, int count)2 _' Y+ b: B2 z0 a7 T/ c
3) public string[] Split(char[] separator, StringSplitOptions options)
& |9 u/ i% ?& l7 M4) public string[] Split(string[] separator, StringSplitOptions options): V2 T8 k$ T, y, }5 W
5) public string[] Split(char[] separator, int count, StringSplitOptions options)' R8 q1 K1 W2 [" m2 ?9 T2 l
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
- n$ {. E, P8 v3 O- e: H% {$ _* j! ]2 e8 P, j
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):4 n% G, x. @8 C+ B* T" a0 ?5 ]3 j9 B
9 k/ J; \6 H3 M) f1 q/ c! k5 \; ]1 v1. public string[] Split(params char[] separator)
, A6 a* b( P0 {- W/ x
! ]) Q4 B1 q) _6 U' N3 i+ E 程序代码
9 P* b& _2 K+ ^* O) }9 T0 n5 Wstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
. S* q( h) c; Jstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}+ {4 e6 R% z, z- z
6 k" O; m1 k0 _3 `8 y
2. public string[] Split(char[] separator, int count)
- R Z4 ~+ b2 C% B7 T v$ _9 b- m, r8 ]2 T. l; h$ u/ G$ p
程序代码
% d6 n1 Y, Z* y8 ~string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
% |2 L% f% Z6 i8 L! Ustring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}8 s2 e ]) V) [' j
% w& T$ p6 C9 _ n2 B6 D6 t
3. public string[] Split(char[] separator, StringSplitOptions options)9 X0 C% J& y0 Y2 f; y
# J3 K& M; C* A" H6 X; a) M) ~; l% | 程序代码
3 e, m$ z0 B& s' H Fstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素% ?. ^9 l5 n, Q8 V& u B, ?# H
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# q# }) L! Z: @9 U6 i" Q. c+ t
( @, ~+ s. k W9 r4 h9 }3 y* o4. public string[] Split(string[] separator, StringSplitOptions options), p5 Z( G; Y+ R1 v2 L8 C1 h, U6 ~& u
$ ~' A$ B5 F7 `
程序代码
* F9 x: ` j" ~string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素& I# |2 i2 B3 ^& O* P
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 ?9 h6 D7 i0 h, O4 x! J& S/ m% r8 y* ^/ v
5. public string[] Split(char[] separator, int count, StringSplitOptions options) z' Q! D; u+ L6 o
& `9 Y3 y2 O" O+ A7 t, |' T5 R 程序代码0 A; H- d7 N2 A+ W
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 E5 h6 v- T0 rstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
4 D7 [. n9 D4 ~! c! U# Q2 t: B3 m" t0 ?+ a2 A
6. public string[] Split(string[] separator, int count, StringSplitOptions options), M2 g# u9 @% J }* A, n
. K' u' L: j8 r; ?
代码
3 n1 Z( {, j+ X7 `. d+ o3 O, z# M1 m ostring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素+ E% o. N7 E( I C: C% W
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |