下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
4 ^3 ~/ z) A; U" x9 Yhttp://www.itwis.com/html/net/c/20100506/8234.html( ]! \: s5 s: S- z; L
$ M( c( b+ Y# Y. U9 {6 V, ?" w4 h0 R程序代码
/ g9 q: [: l* J/ r/ ^& g1) public string[] Split(params char[] separator)
, {. p% }0 r% R" t# n/ A' Y/ O2) public string[] Split(char[] separator, int count)
! ]0 L4 p- o% M( {3 g0 W3) public string[] Split(char[] separator, StringSplitOptions options)3 X6 i8 h- E% w5 D
4) public string[] Split(string[] separator, StringSplitOptions options)
% v: |* \# @9 v. L( L7 j+ u5) public string[] Split(char[] separator, int count, StringSplitOptions options)
8 Z( }5 Z* T: P7 e* f7 {# L3 H* q6) public string[] Split(string[] separator, int count, StringSplitOptions options)7 ?. v B+ O' ~- ^$ a- {
+ l0 {# {3 Q# v* i
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
% c' b4 l) U+ E. d% D
& m* D p6 c- c5 X- o4 W1. public string[] Split(params char[] separator)8 O4 j3 }# l$ ~' \1 ]& S; [
# U2 b+ A4 w! x7 w* ~ 程序代码6 V8 L6 H( {: q. S( q6 p
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}* c; x* E2 a( X
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}* c7 F' C9 B0 X Y
2 Z# i0 G0 Y) X2. public string[] Split(char[] separator, int count)
, `" H1 F% X# o3 ^3 w/ \( }& ^9 }9 D2 C7 H4 V/ S
程序代码
1 r- V1 v" b, p. Rstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}+ S2 A9 R, c* C P5 P8 [
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
1 r0 ^) M/ B2 U# y1 a8 K$ S; S0 W3 D
3. public string[] Split(char[] separator, StringSplitOptions options)2 m( Z7 k, o" j6 h' E1 z
# r; \' c$ z( o/ c4 J% ?
程序代码& K4 t7 `+ \' i# X
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
d+ L/ [% t0 a' ^" {string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6 l/ N' ^$ \2 I( ?0 A7 f1 Q% g. F$ e
' L2 {, w! j# {8 Y. m; Q4. public string[] Split(string[] separator, StringSplitOptions options)
( T* Z0 l0 |6 T" `4 R& ]
$ D# K3 y( K0 n" l* c3 e7 {, ~, E3 a) J 程序代码
, \; `4 I4 Z" F! |9 R3 Astring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
( D1 X+ @3 ~+ H- Kstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
1 H2 o& F5 C: v) P" ]
& R: ~1 p) t& L" P8 h5. public string[] Split(char[] separator, int count, StringSplitOptions options)
0 |0 d. V) S( L0 C$ [# s7 g5 j, q9 m% M8 W
程序代码8 j! h2 g( j) t) m" ^# F
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素6 ]8 a8 |- |6 m6 U3 U$ E( W
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素' H/ T0 I3 e! b* t/ i7 @& t
; R% K/ ^' |5 X6 `0 X6 u+ V6. public string[] Split(string[] separator, int count, StringSplitOptions options)% Y9 b: o$ z* h( Z! M8 u
) W" B$ Y, P* G) M, K0 g
代码
* G3 L/ U# T: T, D% z+ Jstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素6 v+ a& ~) w& ?- ]! S
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |