下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看5 P/ F3 Q( Z. x C! S% K
http://www.itwis.com/html/net/c/20100506/8234.html
! O- l1 T- }/ V1 R# R% H$ f) M
( Z/ I0 h/ z. o" M s; H( b程序代码
) O. g, q8 ` J `2 S5 j* |) Y& d1) public string[] Split(params char[] separator)# z [; r! l: {
2) public string[] Split(char[] separator, int count)
1 g) L& J; k6 W/ c% @3) public string[] Split(char[] separator, StringSplitOptions options)
4 O: ]8 H" ]7 E5 g4) public string[] Split(string[] separator, StringSplitOptions options)
9 j {4 S# {, S+ k! a' o) x5) public string[] Split(char[] separator, int count, StringSplitOptions options)8 a* m& n' D% ^" l2 V8 @9 x! q1 [
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
0 e4 ~5 S% I# L2 i+ Y
, A9 V0 s% z, q8 y! Z, v0 g4 b下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):' l' t9 I4 h; a; l' s7 m+ n
9 C2 {9 }5 L7 t/ a3 O1 G! }( n4 |1. public string[] Split(params char[] separator)* A3 F, g6 C: ]2 I b; e
9 t0 V1 p* g- F
程序代码7 f- V. v+ I7 t2 o, C/ ~0 l
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}: m, G4 \# e1 d/ R" S5 Q
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
9 O4 u; z0 g( F
: ^0 |' `& A; ]) j+ R/ @. ^3 `2. public string[] Split(char[] separator, int count)
' R; A: |' ^1 a( U8 T. @" t, k& o3 K7 B3 g
程序代码
0 C5 c# q) k. i3 s% I3 m" Qstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}* a! v e" w, ~
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
' q3 k1 I( g" _3 U9 K. r9 _" s% b% u
$ f$ G5 P3 {+ {; ?# K( _0 d3. public string[] Split(char[] separator, StringSplitOptions options) J L( X6 b3 x2 ^
1 Q2 e' Z, H' N9 g; |( M6 j; X" @
程序代码. d8 j, c8 u6 k2 F8 r1 }& h' u
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
, a2 A* f9 e4 i/ X9 G9 e( f pstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* N0 e( }* i4 T: R0 T7 t. o2 T' @% G" I
4. public string[] Split(string[] separator, StringSplitOptions options)5 a4 f. C* @* y) f" i N
4 p8 `2 l L+ L" q: C4 T 程序代码( c4 a& w7 R8 b+ O
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素0 x8 m- C+ ]2 H" G7 [/ E J
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 M3 I% U: u) s9 H/ _ o6 t
: `7 }! L: m* W3 G5 Q5. public string[] Split(char[] separator, int count, StringSplitOptions options)0 S5 e) {; E8 s! f5 ~2 b: h+ e, N
4 N5 ?" Q( o/ @7 ~5 R6 [ 程序代码; H0 X7 v' w- ^0 p3 G, ^+ B/ C
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素# z0 k# h' x/ c% E
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 n8 V6 j; e( q, n2 D! A& Z! ?8 o3 ~# j2 ]
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
2 I$ W, f( H/ T2 c" u5 a. w8 c+ }6 K( B
代码7 y$ b7 `' T" B! \/ `8 ~. ?
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
M! ?1 H8 X1 rstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |