下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
7 n& j; \) P) P, }+ m: [http://www.itwis.com/html/net/c/20100506/8234.html
d% t" B: A0 z. u) }
& T! }4 a, x- Q* M2 d! I程序代码, Z5 N7 u; X* d7 C
1) public string[] Split(params char[] separator)5 V- K1 o5 W9 f6 {* w. w
2) public string[] Split(char[] separator, int count)
/ F8 ~" l. ~; e. Z- O/ f3) public string[] Split(char[] separator, StringSplitOptions options)
% X! g2 j* X% h3 D4 W4) public string[] Split(string[] separator, StringSplitOptions options)
" y7 e9 l! ?1 q" K. D+ N0 ` O5) public string[] Split(char[] separator, int count, StringSplitOptions options)
9 _& I; I( K; g) g$ H+ n6 [$ c; C6) public string[] Split(string[] separator, int count, StringSplitOptions options)3 o& C m; s$ v, D! c
* K9 k! }! a f; U6 n1 x- L下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):9 I! y, s% ?9 v6 l: C$ {
0 [2 l9 D6 A1 I) r. D( ~
1. public string[] Split(params char[] separator)7 |7 X, P' c7 x( ?2 o- U
# c: l4 O0 n1 Q9 `8 |! M. U& s 程序代码
+ P1 o% L3 H8 y1 x2 r. pstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}+ e5 u5 a( o* `+ w2 l
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}4 d U) B1 [/ S+ `( ]
3 _6 |9 a) D, K' k2 A
2. public string[] Split(char[] separator, int count)
" Y# S( m W" Q. K
7 @- H6 g- w8 C 程序代码
# E9 W% _7 d# X; r' Ustring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
3 @+ l7 C7 y( Q/ ~+ A0 ?. y$ pstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}# B1 K" l. m3 E1 b
: d$ C; y1 N, m' C$ d$ W& M3. public string[] Split(char[] separator, StringSplitOptions options)! T; x* g5 A9 e
( x! M4 p4 Z% v6 S4 ?
程序代码
9 Q3 R8 y, {9 A9 ]string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
- d3 Z4 m. R4 }2 n% Vstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' G* e; A9 z2 c6 W4 C
, H5 Y* y1 ?2 x+ C' h S% }4. public string[] Split(string[] separator, StringSplitOptions options); E l4 n; t/ @ x8 G# D \8 X: g7 L
$ S+ J, v' A) S3 Z$ Z+ ]) N 程序代码
( q; `8 E# h: ]1 Fstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
5 V" d+ h" W- Q4 s$ tstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6 d8 |9 R( o0 u; x s- Q/ \3 j% s& c) ]( [3 Y) m3 a
5. public string[] Split(char[] separator, int count, StringSplitOptions options)/ U5 P4 ~9 s3 ~- z, `
. j6 a( W5 w r! r D 程序代码
q6 g3 R# P+ X+ |; g" Mstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素' @3 M: K% d$ {, w; M: q D' d4 a
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素9 w% V/ V" M% x# G6 t- z$ l$ T; C
% W: U7 a' V' ?7 w" q0 n3 q
6. public string[] Split(string[] separator, int count, StringSplitOptions options); X/ b8 p, R$ v4 ^0 m
; V0 z: T* D! w; i7 U, @& @" ^8 m2 Y代码' s1 x. |: K9 k7 M7 d. C3 O% B
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素! i1 M/ ~5 D1 G" e
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |