下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
8 z% Q; U; i/ L) [. P: Whttp://www.itwis.com/html/net/c/20100506/8234.html- i9 C, z6 U! h
; j( R4 D* ~9 }! \. f. u程序代码
5 E" W" j2 a( M( N6 g E1) public string[] Split(params char[] separator)
9 X% @8 ?7 C" k$ G: f6 y: W1 J3 y2) public string[] Split(char[] separator, int count)3 Z2 Q& _1 N! S2 _ J
3) public string[] Split(char[] separator, StringSplitOptions options)
! M5 I: [1 Y [/ a) W. p. P4) public string[] Split(string[] separator, StringSplitOptions options)
1 o2 Q9 W- Q7 A: \) Q5) public string[] Split(char[] separator, int count, StringSplitOptions options)
1 W7 @" I1 Y2 e9 [1 i% }& i# Y6) public string[] Split(string[] separator, int count, StringSplitOptions options)
( w, L" N: Y6 K' p- l1 d. ]2 }9 u& J0 M
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* W* w5 P( T$ ]! p
/ |" B: W2 a U3 @8 S
1. public string[] Split(params char[] separator) W2 k' K* A& S- `
) N9 n$ ~/ \$ E' q: R+ v/ W( h0 h2 [6 w
程序代码
9 |; Y+ B m! F( `string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 v' i0 Y2 R3 J/ D' k
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}* Z6 c" d8 t+ w7 Y
; n1 G( V& @( @; p* E4 w2. public string[] Split(char[] separator, int count)
4 D: f9 D; k7 P- M, P
' E; U) \4 ^+ G! }' F+ j8 m 程序代码
w4 |7 r% C# }2 ?8 a3 qstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}" O9 }0 p0 i7 _
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}3 h+ d6 \$ D$ ]' c. T% O; o
; D' |$ M1 o" N! m, i0 {: F
3. public string[] Split(char[] separator, StringSplitOptions options)
U# P( H0 y' Q0 {4 W' f& L+ P+ y p' e/ q
程序代码; ^* D7 U) j% k/ O8 C
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
& N, ?: @% P( X( Jstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, I& X% R4 E' a9 _
/ O: z8 v4 |+ Y2 F7 ?1 z2 g4. public string[] Split(string[] separator, StringSplitOptions options)2 i& D2 E4 W2 h# T5 ~- q/ C0 g @
8 H3 y& S* k2 q5 | 程序代码5 e" U% |: ?4 _
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素/ [: a. P8 e! V9 M5 t, o
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素" N- u4 o$ O$ b$ r8 W1 F& o
; y$ A+ ?- c$ t2 d% e" r5. public string[] Split(char[] separator, int count, StringSplitOptions options)
; F) _" M9 |7 S# n W$ N$ @$ W7 A. Z1 ~ P/ M2 z
程序代码) u9 F1 q% y9 h5 K) v
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
T/ [& j- _1 b( |string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
+ L; I7 S/ ]! x8 W$ ~5 W7 Q7 T: o
1 Z# z4 c! T0 H% A6 ]6. public string[] Split(string[] separator, int count, StringSplitOptions options)
# E8 C5 ^+ b; C [3 H; v2 L O; i3 K1 I
代码& k" z2 ?2 O# L2 P- x: B4 V' d
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素1 n. t/ d' |3 j/ Q! W
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |