下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
4 b6 l: U5 J' ]6 shttp://www.itwis.com/html/net/c/20100506/8234.html
V6 v3 v5 v0 t( O+ m) F W
: O6 Z# ~* u! X5 R u程序代码+ v; N$ h4 H7 o' Y5 w( c
1) public string[] Split(params char[] separator). V" V u* D( z' Q( ]
2) public string[] Split(char[] separator, int count)
' Z* m' j( H1 H C1 v" s3) public string[] Split(char[] separator, StringSplitOptions options)
" r) _2 Y+ a1 E2 h0 r2 T4) public string[] Split(string[] separator, StringSplitOptions options); V: X x+ k/ h
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
# y% H# O- ^: {6) public string[] Split(string[] separator, int count, StringSplitOptions options)7 R6 k9 h, y; Y! P" Q
( j9 T' O8 v+ m9 L! Z) |/ a下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):# p% k7 _( v' C5 c
$ f* I$ e' S$ r) q# |: \
1. public string[] Split(params char[] separator)4 `9 {$ B" Y1 l1 p. ^! a* N* x
% \0 S. d8 O# L 程序代码
. S) F6 C- d' F8 Fstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
g) ]5 K& {: s: Lstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}! `3 |0 c6 N' Q
2 B3 o4 o5 Y3 F! {+ |
2. public string[] Split(char[] separator, int count)
! u; V* O) M! B5 Q* T' K: N& @+ \' I. d1 ^& V
程序代码0 m) i2 _& { d1 M. ~
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}) A& V4 Q* [ U( C3 P
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"} D7 h( H0 Z5 F8 G
" Q& z$ V# ?) a3. public string[] Split(char[] separator, StringSplitOptions options)5 Z6 g4 T9 K2 d, F
1 m/ w! b; \9 [- M$ V
程序代码2 X+ f, {. {, w8 S1 f$ S" Q
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
! ? w. ^! i9 f* w( estring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
8 r& o# B7 E" |0 E% l# k
3 n7 M& {, v2 D; b& j" `4. public string[] Split(string[] separator, StringSplitOptions options), C1 d; c( ]$ |+ W; v7 C7 q) ?
7 s+ g3 |7 v% g& W2 p
程序代码
; B- Z% T# H4 f8 D$ zstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
7 q8 B$ @& f: f7 O) q, nstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# C f8 u, e7 }' E" ?7 f& B
) `7 ], @- g- o: m0 k
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
0 Y" d" s& G* Q1 l4 E. R5 t$ Y; P* C8 w6 \
程序代码5 ]% b* w; Y- m- d
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素7 e3 C2 i- h* _/ i. c
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素: y) l/ @$ L+ F- L" o* c k
' P/ n. c7 Q1 f/ [& j: H
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
. h3 e0 V+ }7 H* ^! t& f ]" i/ O; t6 j9 x: p; }, m8 \
代码. ~) b$ p% B4 @% p
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
1 \2 J# a9 }6 C; f/ ystring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |