下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
5 E' A0 U; V6 B( O& o8 nhttp://www.itwis.com/html/net/c/20100506/8234.html: m0 a: |- J, E8 S, d7 L
* k( L4 o1 D0 ?" i& I9 S9 v程序代码' O* f9 H, m5 v _, P
1) public string[] Split(params char[] separator)
% D* L' W0 @. K# w. x) Y6 r( O2) public string[] Split(char[] separator, int count) e! v" j- }) M
3) public string[] Split(char[] separator, StringSplitOptions options)
: |, w" f3 r# r4) public string[] Split(string[] separator, StringSplitOptions options)) D& b$ V6 e9 F4 i! w
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
) ^9 w; W! B) q8 O( y) n5 C$ ~6) public string[] Split(string[] separator, int count, StringSplitOptions options)
! }( |# Y- H: J' ^; {. }
, Q" b0 {/ n! \8 ?& S下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* i; m! T$ G* z4 e
6 _, G2 A( f6 ~
1. public string[] Split(params char[] separator)) i( T/ A' s$ p$ j5 q5 w( N% @
% i' f4 g3 M; H' b
程序代码
& a1 S8 K) @" S, i8 Ystring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
; w6 H% j5 r$ @$ B6 cstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
' l. S, {- r. K6 ?5 { t" U* G# U* |& K) P* s- Y
2. public string[] Split(char[] separator, int count): w* A6 b( z/ X% f; N* {( }- ^
7 T7 ]; s+ p7 e- S- d
程序代码
9 V9 C% D0 ~9 \6 Y) {, Ustring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
. |8 f5 w$ ?* a; O2 B, Ostring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
( {, i( M& T$ R0 N# y/ V# @
9 [$ W- f! t" h/ u+ ]0 H3 m3. public string[] Split(char[] separator, StringSplitOptions options)
% \+ Z/ h7 G% H9 C' q4 N0 Q* m/ l
程序代码
0 x2 F4 X, [$ ystring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
; x6 W. u/ N, {2 C: b& ~string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
; l" y9 r: F+ r. M7 X( h7 I' H$ s/ T
4. public string[] Split(string[] separator, StringSplitOptions options)* U& m! r4 \. c. @ e% p$ h# }8 g0 T- l
6 T6 F1 u8 F5 _
程序代码& q" ~1 ]% v6 i! Z) Z1 U/ o2 m
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素+ q0 Z* v/ Y9 @9 r! s: S
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 J7 m4 J( v- _% A% y' h8 {2 O: q) G X: G6 r% X6 K" n2 p
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
4 M z3 c3 C% J5 l/ o
- b; m" U% o: T6 E M 程序代码
# o) A6 h- e9 x+ y, c0 @6 Rstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素1 K" q# d' P C) U" G" v+ m6 J
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
A8 a3 }, [# v" b# @+ [' y/ q" X7 X$ o+ x% i- b- }
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
- y5 ?7 M V7 L0 I% {, m
- p; ~8 o6 x/ W% ?. ^+ N/ `代码; w5 Y9 W* k& v7 @$ x" z5 e5 M
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
4 L5 N L9 y: m* ^! wstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |