下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
" ~: P. J9 q, u* `http://www.itwis.com/html/net/c/20100506/8234.html8 X0 I3 }: m7 Q( T! ^. \4 W y( c
H) n( Q& @ A+ Q
程序代码
$ z3 Y+ `0 j- |: @4 O& y1) public string[] Split(params char[] separator)) P5 z" I+ `! @( t: h) X
2) public string[] Split(char[] separator, int count)8 D% Z% d! B9 K2 v8 H" T/ _7 {
3) public string[] Split(char[] separator, StringSplitOptions options)
. t, P4 K" m: C4 z1 J' j7 ?$ r3 H4) public string[] Split(string[] separator, StringSplitOptions options)2 q. t+ Y& j5 h% X/ q O# y: N
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
, e) i, H+ [2 L* a/ h6) public string[] Split(string[] separator, int count, StringSplitOptions options)( M- ^% F" T& \5 U
. B5 d7 {0 v4 ~8 B) z; ^下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* R) s. P- s' O: F2 L* h# r
% c! }0 q/ k7 [6 ^/ \0 Z, o1. public string[] Split(params char[] separator)
; P! ] _ P R4 Q, R% [2 Z" d4 z
. v& ^7 y# y" k- G1 |+ @3 O 程序代码
2 a$ \5 D+ c" |5 Tstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 d! }: z/ @! {4 q, f: R. l
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}% j' P7 d8 Q7 F6 `
: h* u Q* F" g$ @2. public string[] Split(char[] separator, int count)
8 N5 ]+ a7 r G" r* m
4 [& L8 R4 [0 A1 u! k) C 程序代码7 ], Q0 A' T+ R5 G5 |* N. U- r
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
[* b/ V$ m7 n4 S( N& gstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}1 ?1 y7 S3 o+ _. }, b8 e6 a
8 v9 j$ H& w$ A! M* z* e6 k
3. public string[] Split(char[] separator, StringSplitOptions options). T# k/ g) M0 V D/ P% s5 D
+ D+ {: ^, @; g) ]; w. d 程序代码
3 P: n0 U& j0 pstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
; [1 r8 B: R7 V3 o* a& x, j9 i% l4 Gstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 P+ {% P# E0 R9 T
- a9 P4 I- h1 J5 ?; W: A8 I
4. public string[] Split(string[] separator, StringSplitOptions options)
( i3 r/ z* o! @8 a$ S7 k7 [! L* R4 R) g3 h' N. c0 J" K0 o
程序代码
) Y5 x6 o: N8 Z% {$ N( dstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
% ]; m' `0 {; Z. |string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, Z) p! V3 s7 h+ [5 q8 G
0 l( d* X/ k& E: _$ o2 u/ B0 U5. public string[] Split(char[] separator, int count, StringSplitOptions options)4 y# ^" t( x6 _
+ k4 d& Q; m9 ?) r8 n 程序代码
9 ~, Z" r; n5 w3 e! l7 jstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素, h9 Y7 ^ ^ g6 P$ y; R
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( Y1 m% D4 {1 G9 n' P9 R2 ?' s5 g H- H/ @
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
# d% e- ^! y8 E
( n- |# Y- C: A. h9 V6 R; s# y$ o代码, A, \* ` X: s
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
6 |' Y5 ]6 p8 m9 t, m" Cstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |