下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
+ ^# Y% k+ g8 T1 d: z4 C& ohttp://www.itwis.com/html/net/c/20100506/8234.html% \7 f' K+ {5 z# C; `4 J
3 U$ B) O8 |2 a, J! c, A5 k: B: V
程序代码
% a; {. C9 l V1) public string[] Split(params char[] separator). H" L# E8 ]3 e$ i; |4 E+ e0 X) A
2) public string[] Split(char[] separator, int count)
( k L5 {* B( C7 L; @5 G+ E3) public string[] Split(char[] separator, StringSplitOptions options)& z6 Z! L9 d4 ^2 ^% b
4) public string[] Split(string[] separator, StringSplitOptions options)
8 X m s( E% A% h8 d- ^" s5) public string[] Split(char[] separator, int count, StringSplitOptions options)
* W5 P& k9 S% n' X6 r0 Q) a2 ]6) public string[] Split(string[] separator, int count, StringSplitOptions options)
9 X6 ]/ T2 V4 @5 B
8 o. X( m. ~0 `! [2 Z( Q" @" h下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
. ?2 `7 ^# Y( t
1 s& |# Q. D; [0 e: t `2 v% ^1. public string[] Split(params char[] separator)
$ C+ P' @: `* a" v
/ w& g' R8 H+ W7 Y* J& B/ \ 程序代码+ B. x% c& o. {8 W5 {% Z
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}" }6 y/ b9 Z( f) g5 _. K) Q: c
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
! k! l: n$ ]+ p0 r1 u Q" t7 _; E3 a. H5 u: Z1 w
2. public string[] Split(char[] separator, int count)
. X7 S% Y& c0 i# _
$ O5 p1 E# K9 g/ `. d' o2 c. i 程序代码
# x3 r5 X6 }* ^! b. b$ x5 F) Qstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
7 W5 `! K0 q7 S6 f) xstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
4 G8 F* M- W, f+ e! f2 w) Q/ @9 \9 h! o
3. public string[] Split(char[] separator, StringSplitOptions options)
3 j" V" J: g( d- r) W% L
5 v4 j/ I3 a2 X" B 程序代码1 V. G' w j8 c. W. j) v# j# W
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素, A* \/ P: E5 o/ i/ v' P6 V
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
) J! ]* ^( e& z- X4 {# T5 I( x+ _4 h! `5 K/ V; [$ P2 h
4. public string[] Split(string[] separator, StringSplitOptions options)( M4 _, d$ P6 {6 G) b
3 O9 ?; S ?1 ^1 o6 V2 W7 x2 ]$ n
程序代码
h5 l) t, d' v" L1 ?8 qstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素6 c/ B2 Y3 B. Q6 y
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素. `! L( m5 c, w
% _ b" Y' W7 Q( G8 N- J5. public string[] Split(char[] separator, int count, StringSplitOptions options)- k" U- w/ C2 q" B( l
( S, \. J% Z& k7 Z. o3 ` 程序代码
2 j, n' Z C1 C3 o; W5 Istring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# b5 u8 @+ _1 j" u% b- jstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
9 ~% a" Z, K& u+ c
# b6 E b* e- Z6. public string[] Split(string[] separator, int count, StringSplitOptions options)
6 ^, M+ G+ m% h, h
- J0 L: g5 I7 X% u" v" N* s代码! j+ _8 \0 l, v
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素) P- v4 N( S- B& s2 T
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |