下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
4 ^; h5 u. J) u6 z3 phttp://www.itwis.com/html/net/c/20100506/8234.html
# J/ t, J, I' m" m) f
+ }/ E0 Z2 ]9 j/ X. q程序代码
0 I! P9 a8 p$ t0 i: R x1) public string[] Split(params char[] separator). n% B' Q m) V
2) public string[] Split(char[] separator, int count)
( u( X) d% h, S3 N5 H3) public string[] Split(char[] separator, StringSplitOptions options)
( ~+ ~1 U& h4 i, o" V; T4) public string[] Split(string[] separator, StringSplitOptions options)
2 Z# g7 @+ [$ |1 k [0 L& M5) public string[] Split(char[] separator, int count, StringSplitOptions options)& ?; ?4 }+ d9 p0 e2 {+ k8 a
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
+ Y# l/ f% H% y- [" c0 R
; s$ [" c) S/ f* v0 A+ l下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
1 {; c9 ~. S. o" Q4 q3 q2 G- q+ u- A, ]
1. public string[] Split(params char[] separator)
: m2 M! L7 w5 [) w& X* V7 r
, O7 I4 p: l- |3 S7 N. h5 e 程序代码
. y! K+ U' m" Q. r% [3 Q$ H) gstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 N" ]9 w1 U: E6 y. u4 K9 X; W/ W
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
7 M P: U% e9 h; m, l. R
/ R& M) i& P k2. public string[] Split(char[] separator, int count)5 \1 ^! Z' o7 U$ E- e4 w* q
5 c% M6 b( z; U& T0 B+ P
程序代码
8 m! Q! S$ g4 o* Astring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}/ U1 a7 Q) o" \' J+ h, @5 G
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
3 ?6 A5 a) r7 y9 w' @4 \
; J/ C% q7 A- h* ]/ S3. public string[] Split(char[] separator, StringSplitOptions options)" R% N6 a1 e8 w& T
9 ]0 X: ~5 A0 |6 r& H/ S/ _3 i- C
程序代码 L/ f8 z/ F, y8 G' m J$ k* B4 A
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素- R$ F7 u+ Y3 ?7 B) m) D* ~5 ]
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素+ [* B- ^# i( ]" S. f3 M- O
V. s2 e" R. z
4. public string[] Split(string[] separator, StringSplitOptions options)
) k( T2 x! _) q! F# u' C/ W; O8 j, ]: Y6 `3 o- X
程序代码
9 C! u, H) L2 X/ `* lstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
2 D8 j8 b+ A- ]* V8 r! a( t3 nstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! K8 Z$ y. h6 B8 t
' {' d" K9 K ?$ f: p: [
5. public string[] Split(char[] separator, int count, StringSplitOptions options); v6 u9 |, W9 ]; }: S8 v, A% T
$ l8 ~- Q' K# O X8 n# ^6 h& c
程序代码' m, o( h7 |0 {
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素% Z3 o, ~1 n& C& I
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 v. ^; ~) S Q- X, d" g, B3 Q) P. T# U( A4 D
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
6 n5 S d! G3 p% ?# o: C( c7 ]$ v! n1 S4 o+ Q$ e8 X
代码3 C1 k; V5 p1 o* P
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素2 c( @* q9 N% I" Z
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |