下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
3 E6 ?2 V9 T3 L0 i# \5 a0 Z6 W9 yhttp://www.itwis.com/html/net/c/20100506/8234.html
) e) C4 m0 |0 w4 |4 n
6 I- @' \, Z! X' B程序代码7 D2 c4 p/ T) |% a4 ~' Z: }# D" [% p
1) public string[] Split(params char[] separator)
n$ F+ Q9 O: G+ }; M2) public string[] Split(char[] separator, int count)
) Y2 F# V, @" |( R3) public string[] Split(char[] separator, StringSplitOptions options)
; ]+ z7 i9 H) w" f1 w4) public string[] Split(string[] separator, StringSplitOptions options). m! O9 d$ b/ N5 M" L: ?: c
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
' M& a7 H4 {. U5 R6) public string[] Split(string[] separator, int count, StringSplitOptions options)
; M5 ?% d# P0 p% @6 `0 X" K% r A. ~9 A) ^8 J! D5 U; T
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
% I; `' e* [2 V( w" d" M" Q! a7 j; J" B3 ~7 |$ `
1. public string[] Split(params char[] separator)
v: i+ V1 B' B# ?* v
3 b2 g% r$ O3 S5 F( a6 G. H1 v 程序代码2 J. a* _! k y; |, r
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}7 }& I) z$ J9 A4 x
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}& C Z4 O: w( c8 _. W* W
( ^* T4 J. h1 U7 c4 n% w P5 X
2. public string[] Split(char[] separator, int count)% K; u0 L5 _: k% j8 D/ t
& q; K9 G' X6 `, a0 \( M; M: D 程序代码8 q# _- T! o! @) \
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
6 n; R: Q2 E- O1 g: c* Gstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
2 Q2 _3 p0 q" R9 R7 C+ X8 e( p( ?1 ?1 _3 h7 q! w
3. public string[] Split(char[] separator, StringSplitOptions options)
0 j3 G8 T4 @/ H! ^! M0 M
5 ?- F9 u& E% e 程序代码$ c& g' a' K X. T
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素7 t v, d; y. {3 d8 |) |
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素* s" S# o, C4 X' V" M& R
! c4 i* @7 d q) F: }
4. public string[] Split(string[] separator, StringSplitOptions options)
" f& [, m* ]1 K a; [9 y1 i( M) d# \! H, x
程序代码4 G r, k. B" R* @( W
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素) c1 _! P' B5 s
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! U/ S' u" E( Q9 ?) b0 H. X
( |! o$ t# m2 [* t: x
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
, Q; d p4 ~* u. E; I, o% K& g* s: g5 f+ |
程序代码8 O# O5 n9 f2 \ Y
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素1 z; }/ m! M$ Y" T" K1 w G; P
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素) `% V o6 J- t
@8 R# S) `* |) \
6. public string[] Split(string[] separator, int count, StringSplitOptions options)/ }+ y4 M, n" {. b* s k1 l: {
K5 Q$ E$ R% h2 j代码% C* E( e8 h$ g
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素% d7 V, y& _: D. s0 l! x
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |