下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
' c2 k/ k# D4 Y4 ahttp://www.itwis.com/html/net/c/20100506/8234.html
( I$ `6 U& ?3 H8 t2 S C
5 [9 T0 ]7 r5 [3 |3 A1 L程序代码
* f$ o0 i* g S K0 x" w1) public string[] Split(params char[] separator): l# x; T! H+ C5 W4 o
2) public string[] Split(char[] separator, int count)) a+ H/ Z P3 l. _/ P
3) public string[] Split(char[] separator, StringSplitOptions options)0 M% ]+ p( c, J j
4) public string[] Split(string[] separator, StringSplitOptions options)
4 b6 \4 f, G& d. [) y" h* C5) public string[] Split(char[] separator, int count, StringSplitOptions options)
+ Q8 m: j8 y4 q6) public string[] Split(string[] separator, int count, StringSplitOptions options)9 O3 V4 G+ @0 L" p( A( n* e
% q# @! i" T e4 P1 b3 T
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
" u9 P1 a7 S8 e I# D' c
) D' h- Z9 L+ X1. public string[] Split(params char[] separator)
3 R, p3 _" P$ _2 ~9 S4 m
1 L5 `2 f7 f/ J; p1 u; p 程序代码
3 w$ Z o+ w' Ostring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 r9 j: `) s3 r4 K
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
& B" i% ?2 r- E* T0 S/ H7 [ x
h7 t7 ]0 |7 _+ Y! j6 d2. public string[] Split(char[] separator, int count): s( c z4 y$ _. @
0 J6 C+ R6 p O; @: `' f
程序代码
h$ u! Z2 j j3 V* C- Q( x! v; wstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}' D/ z1 g) c, t5 f
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
! b: `0 J% w3 v) W' } L' k$ Z0 }6 [
3. public string[] Split(char[] separator, StringSplitOptions options)4 x" s# c; }& q6 o$ M. v
9 O/ ^* F& ]+ V* n
程序代码: i3 w. `+ x% w7 Z6 ^. b f K
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素( L: h0 H1 o. b/ a+ n+ G
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( N0 i, O, D8 Z& p5 b1 S" x& }- Z2 `- Z- Q9 s. H8 ^1 B& v
4. public string[] Split(string[] separator, StringSplitOptions options)
0 R) j+ K, p: r9 A5 l1 G- N) i' `6 J7 Z! Y
程序代码) R% {* _; e( K X: m- R
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
, E) x2 J" y, V' D: m; w9 d; Fstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# N. H- n) A/ a. k7 o
9 P5 W# q+ X, R# N3 V0 Q
5. public string[] Split(char[] separator, int count, StringSplitOptions options)% h9 N$ c% `, V3 j' A1 c2 t G
) n; C! ~4 n2 U7 G8 j
程序代码
$ O- w5 G0 S% b* `7 @- N" Dstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
: a h) g- T9 Q; y* Zstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ H: s/ ^2 D9 I A; {9 b
5 U0 T5 D2 D( W; ?6. public string[] Split(string[] separator, int count, StringSplitOptions options)5 s: C. N n1 U+ N; o
3 g3 ^* A( _8 I" u4 ^8 I) z代码
/ Z5 o; ^7 v6 J2 n, astring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
' h8 k' f& m5 D, F9 v2 `string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |