下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
S1 W0 @8 B$ h% d) Jhttp://www.itwis.com/html/net/c/20100506/8234.html3 Z3 }9 c, U' {
0 z9 k, D$ \& f. z
程序代码# `6 a" x+ C' |4 k
1) public string[] Split(params char[] separator)$ {% \: ^8 ]/ a: i q( k
2) public string[] Split(char[] separator, int count)
9 p- W0 n: J4 K+ `3) public string[] Split(char[] separator, StringSplitOptions options)
6 B5 p8 |# i* K4) public string[] Split(string[] separator, StringSplitOptions options)
. {; _# J& \0 c# m, i j) z$ |0 i5) public string[] Split(char[] separator, int count, StringSplitOptions options)
7 e( `! b2 i8 F1 d. w- q% [6) public string[] Split(string[] separator, int count, StringSplitOptions options)
3 x5 k+ } Z4 J' r, x. [; o8 P
. E5 M7 z! E0 |下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):! L7 T, c5 p; b0 f* g
: _( z- V* R1 Q) c: N% r
1. public string[] Split(params char[] separator): b8 d" o7 D- t" @- o
1 w/ S2 X+ j" P x Q 程序代码
! S+ c8 j" Q8 |6 Gstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
U h) A$ w: P! S* _* C% Nstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
, m: |$ i3 u- Z+ k3 M2 m
: [3 A* s% I; F$ W# n4 M2. public string[] Split(char[] separator, int count)# T; o# ?* A4 M
6 o% s" `& p- e, u4 f' _% j 程序代码( m5 c; p; g0 f4 \- E4 C- X5 T- A! s
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
; c1 _1 g. W1 W% jstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}1 v& s% n' F$ B$ g3 d' D8 H }( t
. X$ i4 v( k q& _; G% S
3. public string[] Split(char[] separator, StringSplitOptions options). q: j/ U3 I9 ]( r9 ^: F+ f
' N$ X5 c4 t% C6 X* i# H" t1 Q4 Y
程序代码
[$ h; T9 u2 ~string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
* }" `4 G: @: |4 ]; E. [) P& v' O% ^( astring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
8 |' s: y9 X8 p& E2 H( ~) z0 [
4. public string[] Split(string[] separator, StringSplitOptions options)
; b; D/ g, A# r- i8 V
0 N: a* d+ J% [0 q 程序代码
% D2 h: d9 U+ x. V, M% A. Nstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
2 u& s( n- I. R* U3 q3 M1 r7 xstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
; u7 k( s( T" ^5 ~
- P: K( `. p7 v/ i/ y5 X3 W5. public string[] Split(char[] separator, int count, StringSplitOptions options)
* z F( Y, H* c% G0 D- f/ n3 _( }
& @( a! y7 ~! K, Z! @5 w" @ 程序代码
% j" I" j7 l5 M; gstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
/ N" r- M! B; U$ [5 x, zstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% s {# _; b7 e: y* F7 ]# i6 S
y# E% F; g. k9 ^3 h) M9 U6. public string[] Split(string[] separator, int count, StringSplitOptions options)) s/ z5 c: h' [: @0 Y- t7 ^& U
, c3 Y; {7 m& T |代码" @% }" n& `" k9 s* ? b! R5 n
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素0 E" ^; D/ X/ R4 y
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |