下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看& W" P4 Z! l4 B( c$ C
http://www.itwis.com/html/net/c/20100506/8234.html
8 d7 H. N$ p! J6 I
) u" W) Q" V& L程序代码
/ \# Y6 M5 y5 o: F4 y! _1) public string[] Split(params char[] separator)
4 [: G6 j* S) p+ g2) public string[] Split(char[] separator, int count)
$ T0 G% ?6 q& W" @0 T8 r3) public string[] Split(char[] separator, StringSplitOptions options)
K$ x% _0 m# x# N4) public string[] Split(string[] separator, StringSplitOptions options)- D* O6 f' a; h; G" Y4 b
5) public string[] Split(char[] separator, int count, StringSplitOptions options)* W& L8 L0 n( P. G
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
, b4 Z& U$ s9 ?, G6 ^4 H6 i
6 U. T, i6 T o( q; y" ~下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):, ^6 H- Q. W8 C9 t# w
5 e* }5 h4 j* I4 P1. public string[] Split(params char[] separator)
7 G' u) @: Z- Y* x( a- C& t: s* ]1 P* W& d( V2 d( f( Q. Y
程序代码# N" E2 {3 d. N7 Q2 S K1 v {
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
# x! |! k2 ~* |- A Jstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}3 ?. [( L, l5 S1 c
8 t# F! k! {& ?2. public string[] Split(char[] separator, int count) C; {/ }& M4 h
; J5 s4 \, w2 z! l" B! |5 O$ k
程序代码
4 J! T/ e1 T7 I3 i+ |* h5 f1 t, y( astring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
- w( d" W1 z8 J" fstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}2 K5 i/ U B' T) Z5 l& l! d! v# O
) p: f& t# C6 F( n! C7 e
3. public string[] Split(char[] separator, StringSplitOptions options); p% o; p! w% O5 b9 J
- k" A$ b, ^. f: \
程序代码$ y' j/ x9 E8 G+ ~4 m# a
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
2 [( O2 v) T" o7 fstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素8 s3 E" L& v3 Q2 V3 T
g2 N3 ~ d y/ @4 Q
4. public string[] Split(string[] separator, StringSplitOptions options)
! r% r( n! W) c, \- z/ T5 t
/ i+ N" X( x* [& d5 h+ w 程序代码. \) Q; F, \9 b
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
/ o; }: f U! R3 f7 O& r2 Fstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ Z' m8 w# B& t
# H5 }* ?" `# X6 T5. public string[] Split(char[] separator, int count, StringSplitOptions options)1 j6 K: ~% V; ]# u( H0 `
. D2 Q+ b7 j5 A; a0 a& j K1 X+ ^
程序代码8 `, a! X5 ?# ^" {6 A, W
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
( A" S* A2 Z# N$ rstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, F0 a. G# [. @9 J) J( u8 G! N
* S& w' P1 E j& S6. public string[] Split(string[] separator, int count, StringSplitOptions options) [3 L" d* N0 ~
" x6 T( {2 L- K) T; J) w( z4 T代码
9 r4 D$ j9 h& s. u, }! Dstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素* I9 R0 F& X& H& M1 I: @4 J! ^$ _
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |