下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看! x) S/ J$ F$ v
http://www.itwis.com/html/net/c/20100506/8234.html
0 Z9 g6 ?- s+ _0 ^1 r6 O" r5 w0 |3 I/ p F8 v; X( i
程序代码6 V9 i W ~- ~! S# j% H5 E1 L
1) public string[] Split(params char[] separator)
7 v, f, ~* U* T, V6 q; r2) public string[] Split(char[] separator, int count)8 J, T( L7 G; h7 H8 v" ~' l
3) public string[] Split(char[] separator, StringSplitOptions options)4 m% I( o1 O, R- L/ w% Y7 t
4) public string[] Split(string[] separator, StringSplitOptions options)! f5 x6 I0 p8 M
5) public string[] Split(char[] separator, int count, StringSplitOptions options)8 }& B# T9 z1 m9 [3 v/ \3 v$ z
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
4 @& `" m# U1 N9 s$ U5 I% T& S
M# Y( t* O9 A5 o% b+ ~下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
/ l6 b6 l% P2 r/ ^, X' y6 a! x8 i
; z- E) P' `4 V6 |+ p( g2 Y1. public string[] Split(params char[] separator). R) S3 r' e$ |) n
6 H3 Q9 d* s1 H, J
程序代码
]: @1 g9 `5 E5 ] Jstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
; t) W) K& r' i5 }string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}. ?, m4 m" M z+ S* L n0 b
8 O! k+ T x' T. w% r1 t( f2. public string[] Split(char[] separator, int count)$ u2 h+ z) z! h+ D
" a1 C+ o- K4 C0 A- D; E; a8 e4 T
程序代码
* a }# q8 v# p# c( fstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}# s1 \- K7 {* X: {5 S! I3 o& {1 W0 h
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}- V5 F) R1 {- `! c
$ n. }5 ?3 A! U; [0 P% d- V3. public string[] Split(char[] separator, StringSplitOptions options)
$ L0 W# c' b# R5 X
3 ^! ~) L; X8 C7 T$ L 程序代码
, F. y/ U- K: t6 L% b) hstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素9 `0 e4 q+ w- O, E: u/ R
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素' d& S* s3 @ D% [' b
6 u& H. Z C4 F' B5 R5 _7 f
4. public string[] Split(string[] separator, StringSplitOptions options)8 X2 S1 \6 p% F' A2 w3 Q2 C7 s6 F
6 ^9 _' |, ~) X) r+ j 程序代码; u5 {% i+ }9 [0 s- X7 b9 j
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
# ]" Q' Q" W1 }% l. m& gstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* b( l: x- T# v, s- }9 p% g( s7 M3 s! P. S; ?! Y4 M! P
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
; c- J1 L6 m( o `3 n
3 {: D( ~5 \& A4 r0 f# D 程序代码
( k& b7 e9 P. M' H$ R) Istring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素( A4 L4 r4 i& I6 F- O; N6 I' o: D! d
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素; x; K) H* P5 {6 V8 N
% g8 D. ]2 s+ [* r! W9 ? V6. public string[] Split(string[] separator, int count, StringSplitOptions options)+ I( [. ~' _6 U- O. m
1 B( }! J9 j( `' C; M代码) P; ^9 v- e c, v
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素 O$ i. \$ o. R" u. {4 A) q
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |