下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
0 @ U$ g" U. y6 k, Jhttp://www.itwis.com/html/net/c/20100506/8234.html
6 O: M$ E- k4 Y. a: I5 A9 X2 c) a* l# T0 ?' d9 g& o" i- b, O( l
程序代码5 [& J M' G) w0 |
1) public string[] Split(params char[] separator)
8 `4 o& S! [5 b2) public string[] Split(char[] separator, int count)
% i# ~4 a4 z; o" P& U3 M3) public string[] Split(char[] separator, StringSplitOptions options) D: S& W* C/ C! B
4) public string[] Split(string[] separator, StringSplitOptions options)5 f9 w: N8 l1 o; M$ X- }% ~ K
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
; k) R: I% K! o6 d0 Q7 u# T6) public string[] Split(string[] separator, int count, StringSplitOptions options)
0 }( Q9 }1 P' U4 U+ o. u, r6 D! \" a2 G9 k0 _. s
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* z- y6 @7 W- {; k
9 F, b5 J/ h, p9 c7 x3 o8 T0 r: a
1. public string[] Split(params char[] separator)' l! C1 |7 a' q' g" O3 a
( r$ Q- p+ u, ~$ n5 j; U0 K$ O
程序代码
k1 {& v. }3 astring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}, _4 E% l' w( W. r
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
6 }" y9 k! k6 y% f$ P% z+ y ^, u/ Y& F/ c O7 B# @
2. public string[] Split(char[] separator, int count)9 [1 j" A8 `; A3 F
2 }+ R1 T; s+ v+ R0 b
程序代码% q& O5 D( Q; P
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
! Z- n0 C5 G5 g& Estring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
1 O; T' x0 S: Z9 y, n' N
T6 r/ c ?# {, X3. public string[] Split(char[] separator, StringSplitOptions options)
8 F9 L& B: T5 v+ a H3 ?3 Z
/ t+ ?( y4 v; j+ c/ K2 \ 程序代码0 U5 a! I6 [ C$ Q7 L
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# ~3 ~& A' x8 K- Y; \
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素8 s2 z: T8 @3 _) G ]
; b: i$ u" w8 j5 g
4. public string[] Split(string[] separator, StringSplitOptions options)
+ E- ?& D9 c4 \8 m' Y6 x, c0 w& p! |6 R) {3 o3 V8 \
程序代码
* X* g' o! |4 @0 K, B7 S' estring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素& x7 D- a# L* U: d6 |. r
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素. C! d) J) u1 j6 y! M5 \
+ p$ x9 {& M$ N$ j& d4 r5. public string[] Split(char[] separator, int count, StringSplitOptions options) m8 \; b& C$ p8 x; l$ W$ |& B
+ k6 o/ K' v3 p) G3 w1 x. @$ p7 w
程序代码
3 y% l h6 o0 N ^9 ~4 Dstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# I; @9 d, t; ^9 V) G. B4 A/ w1 ?string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5 r; s+ \$ F- D5 N7 s. M8 g" W6 U8 Y% i' p; F
6. public string[] Split(string[] separator, int count, StringSplitOptions options)0 O6 I( d. t4 O2 q! w
& ^( z! `$ e( e代码, {- T7 z* `" D, W. ]% _1 A4 o
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素( k2 a9 H" @5 c" C
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |