下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看! u6 S8 K( {1 b2 f1 }. i. F/ O( N7 o3 q
http://www.itwis.com/html/net/c/20100506/8234.html# S# |5 M9 B. P
$ H3 X9 t1 ]5 T/ x4 I
程序代码, q2 ~0 o9 `4 s
1) public string[] Split(params char[] separator)
, ?$ U, v& B+ ]5 ^2) public string[] Split(char[] separator, int count), n1 S* B4 }7 A F" Q& k
3) public string[] Split(char[] separator, StringSplitOptions options)9 b- J% e1 @" f' V. d
4) public string[] Split(string[] separator, StringSplitOptions options)! ~- M& r1 g8 `
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
7 R$ s; D5 i/ O8 L9 Q& B6) public string[] Split(string[] separator, int count, StringSplitOptions options)
( W$ A* H. P7 ^
% H1 c* e( X! P. @下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
/ T% I9 D& i3 Y" A: g/ J
3 G0 w! t* V, g- B$ G. |, |. @1. public string[] Split(params char[] separator)
) ]% [6 U9 I. E+ t, v' c$ b/ ]
, X3 t p( d/ i$ N+ J 程序代码
: p4 [% w) L& c4 R, b) e8 xstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
7 W. i1 H1 q3 D0 H' a( x' hstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}" s* f. _$ ^+ G" f* {' V8 J9 ~
- s- V6 e6 C) l* B2. public string[] Split(char[] separator, int count)0 H( A8 g! n/ e0 @$ J( s) V& ^5 t
* [8 o) ~, l5 v9 V9 [8 }0 A, o 程序代码$ F: p9 C2 x) T) J
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}4 M h) R- W0 s' p: t' D
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
H: y7 g5 Q, J3 V b0 h1 E- f' H7 b7 Z) f
3. public string[] Split(char[] separator, StringSplitOptions options)
+ U+ R* M, U" W6 s! ^
, x. [( Y% c, R- y" |' o 程序代码9 K% |% ]& r% \ k
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
5 v! c3 O T1 {string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: y; L4 |& M+ d8 m; K% Z
' w- A9 W4 G3 S( X( F' J4. public string[] Split(string[] separator, StringSplitOptions options)) S# Y/ B3 B1 H% k! N5 n
! ~/ C2 K; K5 y# {; j
程序代码+ i* r% f, k+ n
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
; Z( B, H1 E0 k7 \1 M+ D3 T. H! Zstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! q. k; d I# `6 R
( u, Y; @. B' c4 b. r5 e
5. public string[] Split(char[] separator, int count, StringSplitOptions options)! J1 H w+ V9 m0 K0 }; i% z
3 V, Q5 F! a! F2 A
程序代码+ h b) I; J; X$ T
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素& @/ F. W6 ?; J3 w1 \, s
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
- A' s( Y; h4 v+ O: A* [6 o! q
2 O" L6 N4 v2 H7 A+ F6. public string[] Split(string[] separator, int count, StringSplitOptions options)
$ p$ r* G8 b0 H& \' E5 T* ^- L& X: M( S/ D p3 w. W
代码
- P; i! c# E7 X9 y7 `9 E/ Fstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素0 q5 g7 p6 N% ]" B
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |