下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看9 ?, i, V, ?3 t1 j' i
http://www.itwis.com/html/net/c/20100506/8234.html
0 B5 N7 u9 y# [; k7 i, L' u! [$ s
/ y' \' Z; K. d$ I4 O" y程序代码
6 X( ^* @+ i0 ]& b0 m9 D1) public string[] Split(params char[] separator)
3 f0 r2 k8 ~$ Q" X2) public string[] Split(char[] separator, int count)2 K6 R$ f/ X% v
3) public string[] Split(char[] separator, StringSplitOptions options), d( @! R- O; Z J0 c2 x( U
4) public string[] Split(string[] separator, StringSplitOptions options)0 V) o$ }( e. b; Z
5) public string[] Split(char[] separator, int count, StringSplitOptions options); |4 F9 h5 p4 e1 ^
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
4 A& b7 \: F! S
- F4 X' l3 Q; D- r) d下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
" I* a1 [# s% C7 f: e# D1 ^+ s- Z1 p8 f
1. public string[] Split(params char[] separator)* l' T' X/ R# H" ^7 B
2 T& O7 C8 M# A, f
程序代码" V. b0 H4 p' a% O$ d, P+ p
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}& }& |* D$ I7 z% u
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"} Y/ _0 s' _3 O
+ D) h# w8 X; C2. public string[] Split(char[] separator, int count)
0 |$ n/ L$ a% q' T w3 b% d/ M
; p( h! Y$ K' {, X2 x 程序代码
$ p% x. B0 Q; W" t) z Bstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
2 h4 K' v3 v6 s [7 R& Astring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}( `/ R+ X% S: x
% u' S B! v; [7 ?
3. public string[] Split(char[] separator, StringSplitOptions options)4 a; w0 t3 b: C% P( F# K$ E( H
& B% y# ?7 Q& r3 m4 V
程序代码
$ V& M- r9 y. A- u* s6 [9 Dstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素6 D5 d' M0 j H( L7 R
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素8 C! w( y5 J: u5 N3 {" L
: p. b5 g# J- s4. public string[] Split(string[] separator, StringSplitOptions options)2 e+ e9 M- J3 J R; G
& W3 p: x% a5 b3 G- u
程序代码, r- _" y3 Q; [' _) V6 c
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
. `1 U; k6 f# I/ V1 P5 K$ Ostring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
, Y0 x0 N% ?; n T) P, X" V5 d$ N$ @
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
/ z2 L1 d' h& M# D; s
9 W; h* h7 \; E' G 程序代码
+ k k) c0 i ]5 d7 ?1 Gstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
) M5 l* t* T7 ~) O* \* q, Bstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
, Y* C+ G. ]) U2 t6 l/ J( ]$ q$ s* Q1 g% j
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
$ q* @6 i" s; ^! b' I0 ^0 V# S: k9 I. u# M% Y8 Y" a
代码% D& b" A9 ^! X3 j7 x a- _
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
5 v4 t0 y. {+ T6 X" P9 Estring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |