下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
5 v& d& N2 j5 Vhttp://www.itwis.com/html/net/c/20100506/8234.html
+ V, I. C0 \7 e) V$ M1 L
( R$ q2 D# p- ?- o程序代码* O4 }% \2 i1 _4 k4 J9 K
1) public string[] Split(params char[] separator)
! z. j* }' X6 P5 U3 _* k2) public string[] Split(char[] separator, int count)
3 u& c- K5 X, W d7 c% M5 t% S3) public string[] Split(char[] separator, StringSplitOptions options)) _7 Z, u1 f8 }" Z8 K3 b! W2 W
4) public string[] Split(string[] separator, StringSplitOptions options)7 Y" a: k4 S' ^1 B1 k
5) public string[] Split(char[] separator, int count, StringSplitOptions options)4 N0 }3 w& v; R2 V: }+ T: N
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
/ \ Q" f5 x1 A
8 n9 g, H; m9 P- \/ r下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
3 w+ j9 x7 M4 M
0 k6 Z+ }- j; f+ p, ]7 P1. public string[] Split(params char[] separator)5 c# p$ m' _% C1 M1 T
: p2 ?- ]: b- |0 O1 k
程序代码
/ d* T! Q% y3 j3 c3 w* F" Fstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
9 f9 u$ {) \8 Z1 astring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
B! Q I/ N5 g9 U+ R# z% ^3 m! f- |
2. public string[] Split(char[] separator, int count)
- ~2 b, l1 j6 z' `* U; H R. S4 V! S
程序代码
+ I! I( k6 x" Bstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
, W' N, i5 x3 I5 V5 dstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}/ W2 H ]0 z0 C P+ h
- }" A3 ?* I# |2 H* p# F# j. D! v3. public string[] Split(char[] separator, StringSplitOptions options)) g6 z) b8 r+ c% X
8 T4 I. Y. K+ i" B
程序代码1 a3 N' @- x) x1 `* ]% |- T& F9 J
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素/ i, ]$ W& @# }# C6 s
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素& h a, z, y; c+ B% j8 J+ B C
! {7 Y3 B2 ^' B" P2 _! B x
4. public string[] Split(string[] separator, StringSplitOptions options), y7 _% ~. R* R; `& |% \* j. m
6 J: v, C* J. d7 H
程序代码& r* ?/ q( l( n: F r& |
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素, o2 ]2 ~9 N6 N9 n
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
/ w, F! _; H- ?& ^$ J! s, h( d% ` R
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
" {/ D1 k g# k
3 H" D0 R5 R8 R& ^+ _7 W 程序代码5 v& B: E" ^0 {3 ]/ F
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素( W# q; }' |$ G) [; N4 A
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 {' S6 [& L/ L+ l, @6 Y4 M
! o( n, v1 |' S8 \* ]! @8 e6. public string[] Split(string[] separator, int count, StringSplitOptions options)6 i5 L) S/ m) O
+ K E A9 v' A8 l
代码" E, ?( p% O0 }% C' ~% f
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 u2 d9 e) X- G' t% J' `) ystring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |