下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看+ f$ l( u1 e$ B) S9 z5 h1 {# }3 W* B% R
http://www.itwis.com/html/net/c/20100506/8234.html( n1 H- N, o- i4 o: }6 I# ~9 e( Q0 ~
8 u& k/ _* f- w O5 A, h
程序代码
$ \9 `8 h) G" X( m: I' X1) public string[] Split(params char[] separator)7 A4 N. ?- D! Y/ o5 L1 r1 D
2) public string[] Split(char[] separator, int count)
; X- k3 K. s, g3) public string[] Split(char[] separator, StringSplitOptions options)
& l. @! y+ Z0 t4) public string[] Split(string[] separator, StringSplitOptions options)
: `2 v; g# W3 e l5) public string[] Split(char[] separator, int count, StringSplitOptions options)
7 z9 T9 @! U g5 x4 }6) public string[] Split(string[] separator, int count, StringSplitOptions options)
- }2 |7 _% O8 _3 K( v) f, r1 w5 e. e. F
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):4 A! b9 _- B7 q
* O( C3 D0 `* v7 ~
1. public string[] Split(params char[] separator): l2 N+ @8 _( P. y3 f1 P, d
7 t$ [# X6 C# { 程序代码 W- z3 D8 w( t7 K$ R# |1 | Z
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}/ ~ J' o5 K. o; o. C! Y
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}3 }! U/ h; Y; X1 V1 Y( w8 y
" q0 }, d, g& l! d# x
2. public string[] Split(char[] separator, int count)% a: i" T# v# _( r( r4 {1 Z5 Z9 ^
8 A! \5 k) V4 V$ p1 o. i& M
程序代码5 t& Z& ?8 j5 D" ~# b! W7 F6 u
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}/ |, d. O9 b8 t3 ~& D$ h' g
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
2 U8 @# l* @- ]) a, N5 T% ~- T# ]
/ W2 q" r" e' s1 G3. public string[] Split(char[] separator, StringSplitOptions options)
% ]* ?/ B& _8 q, A$ i& M: X' I4 e% Z W5 c, C' p$ ~* t
程序代码4 A( y" w( Q$ @; q: j1 v0 r- v
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
2 ]4 [0 b- m. K& |. j% rstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素6 q1 U! O3 o7 z) V6 Q0 {9 a0 r+ y
4 L! H s% Z. x/ Q8 P4. public string[] Split(string[] separator, StringSplitOptions options)
1 J) B1 m& A+ B7 J/ t2 m$ f1 m) {! y6 s! m6 }2 f
程序代码
" f' w8 ~. r, y; T0 D) O* zstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
3 q. e+ _; j# m- \2 T7 o+ ]string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# q2 a5 M8 ]6 i( b s
4 _* A+ ?% I0 E" A# m
5. public string[] Split(char[] separator, int count, StringSplitOptions options)0 Q. O q, c1 O3 _* j% p: e! a, f
& P# R+ ^% M8 O% K 程序代码
6 y9 \2 |0 h+ C6 X7 \string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素" m) z3 u2 @0 ]
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
) ~" H9 q0 S& d% v( ?* R% ]( O4 E) `9 [8 O
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
& ?6 {4 L+ i; Z1 x& k( T! B! Y% j( W* ^2 E' m: G. _3 w( [8 B
代码
U) Z7 d2 w" [5 D& s; _string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素7 D0 a' k; H0 V( |7 ~# w* w" [
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |