下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看0 y% O4 u4 b4 `/ e
http://www.itwis.com/html/net/c/20100506/8234.html9 M& N7 u; t/ U
0 |. ]! n' \3 X6 C( R
程序代码
4 }9 Q$ h! [$ S0 I7 ~3 e2 O; X1) public string[] Split(params char[] separator)/ J! I! y( v0 d4 U" u4 U0 |! L6 V0 c$ Y" J
2) public string[] Split(char[] separator, int count)
3 [- H N9 _3 D6 ?( C3) public string[] Split(char[] separator, StringSplitOptions options)
* d* W+ p6 z- P/ y4 e4) public string[] Split(string[] separator, StringSplitOptions options)
: [" v7 P0 g" V- f, C5) public string[] Split(char[] separator, int count, StringSplitOptions options)
w j% q9 `/ p+ S6) public string[] Split(string[] separator, int count, StringSplitOptions options)
* S! U, m8 y0 F5 b; [& m9 S- n- L' G @
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):$ \3 v* K8 t. U3 |- e
2 W0 }8 l) y5 C) z( O' u, W2 f1. public string[] Split(params char[] separator)5 ^1 R0 A; ?) ?- @7 X% o+ `
6 |8 f U+ {" M! _& P0 H j
程序代码+ w% g; g! m+ ^2 r6 }$ y* ?4 w2 L
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}0 a9 o) Z: h& M3 I9 P
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}2 n. R5 w1 l4 W
8 [! X! p% k+ c* l! [2 ]
2. public string[] Split(char[] separator, int count)* g9 t: z( r6 w$ D
9 @4 L) t: M7 \! d
程序代码1 G- t0 u4 O: b8 C3 c. x& P8 L2 ~* p
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}6 |$ ]5 ?; W/ f+ C' f
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}* \3 _: N8 _" I' P* h" J. A3 M
( n0 v7 j7 S5 N# [4 p# @3. public string[] Split(char[] separator, StringSplitOptions options)
0 |, K3 ^- c# v; [4 e' M S% B% R' m" z
程序代码; k$ v" k/ S6 {1 R) H4 f: \. q
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
9 X# L& y' W5 Istring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素) P& K1 U. f. @- g7 z
, C8 B3 L& T L. ^- M# L/ J
4. public string[] Split(string[] separator, StringSplitOptions options)
9 H' `0 f1 K; ]! ]5 D7 G9 q$ N6 F: c( p$ @: V
程序代码7 S( r4 B3 o5 v) i9 T( j" F
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
. U7 Y7 k) p# c8 i) sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素5 J! D7 Z9 i, e, S! c! E2 G. I
' w* @2 e8 e/ r$ H
5. public string[] Split(char[] separator, int count, StringSplitOptions options)' v$ C5 E/ Z( f3 }6 y
' w5 W k: g4 E' U" Z* Q9 Q7 X
程序代码6 I0 T7 Y7 w5 P2 D1 u
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
9 U; u4 {1 d o# lstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素2 H1 C9 D- N" A: h$ j' x9 G
8 V" W6 J" W; u+ v
6. public string[] Split(string[] separator, int count, StringSplitOptions options), ]1 |- T1 A% Q A, V$ V
\, n e! D0 m9 }; t, M* v2 ^
代码4 l9 y: b+ @8 @. [, f
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
6 H+ X/ r; r# a+ Hstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |