下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看7 M6 [' S# C& v8 j
http://www.itwis.com/html/net/c/20100506/8234.html5 D# B1 p# K$ G6 l
8 K( A3 C2 o$ E" u4 u
程序代码- M2 r: V; y# [1 E, M+ h( u% @2 B9 B3 p
1) public string[] Split(params char[] separator)
) C6 d; i- ?( p* G2) public string[] Split(char[] separator, int count)
2 H( A" B& o; a) ]; R( `$ A3) public string[] Split(char[] separator, StringSplitOptions options)3 C" H, S4 M2 z
4) public string[] Split(string[] separator, StringSplitOptions options)
: }( B7 ]' E9 r. J" m& {5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6 x- F1 v5 T1 l9 ^1 N6) public string[] Split(string[] separator, int count, StringSplitOptions options)! ^* v+ w; T! k6 _) w6 R2 }& r
" N. j" A/ p. y% l4 }/ h
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):; ` S3 N/ ~* M5 M# y4 ?0 _
2 P8 i& t' X8 s7 a" I' _- Z
1. public string[] Split(params char[] separator)
' j6 W. x9 h4 k" L* L0 c& ^* n( [% r0 k
程序代码+ H' ~8 G) ~ Y) {, y
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
' I* d0 H+ i/ o9 A6 p3 qstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
8 z+ o8 G; l1 H3 i; C' [) P
9 |+ \) @* q/ c0 ]3 T2. public string[] Split(char[] separator, int count)
' Z4 L0 H+ @" K1 @* k. y, m3 O/ O. {! e* [
程序代码
$ C% u, U# e+ X) P! n/ hstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
% D( G7 J' M0 H' T& sstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}# C5 S6 Y! F# p+ u D' U+ V
1 Q; V' |1 R! x, N9 X
3. public string[] Split(char[] separator, StringSplitOptions options)
6 G% C* G0 C/ j/ ?, a) p& P- B2 r0 k; O% E2 j i
程序代码
) Z/ P4 \- A: s8 nstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
+ V$ n' u5 C1 Z* W4 K2 Q9 r3 tstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
1 d6 Q, f5 o7 U4 _; X# P1 m) x: f# L4 J9 L* Y
4. public string[] Split(string[] separator, StringSplitOptions options)
@$ N0 B2 e, }4 \% x) a
7 F6 `! d7 [2 e h3 A 程序代码
9 l0 s: N) Q5 Y; j( a1 I, W/ vstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
% G+ A. P d' k" }$ e/ ?string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
9 t1 V7 o. K( M: f% q' k
7 O+ i; x( S& m& y% Z% b6 c& J. y$ K5. public string[] Split(char[] separator, int count, StringSplitOptions options)
. b$ }8 X# g4 X0 E" L: W$ g7 b& `4 _
程序代码
4 _0 U+ f& d& F0 Jstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素/ y L- K) t3 b1 ]* K |/ S( O* |* k
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# o* K8 O; S' Q0 W# n: ?% c. `+ |& d. x
5 h3 @1 o; Z! S1 i8 m- \7 b6. public string[] Split(string[] separator, int count, StringSplitOptions options)
7 G. l4 Z* I- W+ V0 y9 W; s6 E
& v4 x0 {0 T2 x O w+ K3 ^1 |3 L代码 H) o; I. A" P1 q
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素# F) y/ j) T$ Q; t# G' ]% e
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |