下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看( d, Q# p4 J! O3 B n
http://www.itwis.com/html/net/c/20100506/8234.html
& x- f% K) T7 `3 I a' o: J% n6 ~5 d# J5 o2 f% b
程序代码
6 h& z u. H$ y0 g1) public string[] Split(params char[] separator)
& a& u1 _8 Z# Q! G# z# |' U+ v+ U& S2) public string[] Split(char[] separator, int count)
8 K$ j) Z- J2 z; t7 ?$ T0 z3) public string[] Split(char[] separator, StringSplitOptions options)
- d. G2 ]: S- q# P4) public string[] Split(string[] separator, StringSplitOptions options) {! S: V5 x, N8 J" T4 W
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
: H' |/ y8 {) e; \" t6) public string[] Split(string[] separator, int count, StringSplitOptions options)
% o7 }4 D4 M. E h' U3 ]- O& g' o( \ p6 Q l1 r
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):2 g5 [, ^: ]3 O5 M
: W, t% k, x* L4 |1. public string[] Split(params char[] separator)' `# F2 S9 H& a. Y! a' |' A9 `9 W
3 @9 z5 s5 ^; F5 ^ 程序代码4 J: t0 O7 T7 N; ~4 o
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}* w: F. c9 ^; g2 i
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}, m- D+ U! w6 b% H7 R. X
# Z/ s) g% p. d% U( a! q* r( b2. public string[] Split(char[] separator, int count)
$ {! o( o: Q1 f% \4 @
/ s! F2 P9 P, s2 O O+ Q, \ 程序代码' a* X3 I. a C. S
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
2 V6 R! j& x; U* j, T5 s o! s. O9 x0 Z* Gstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
% t1 w# v5 a9 g' ~) u, {$ Y# s+ d1 H7 v
+ Z1 `9 L: V. R$ p3. public string[] Split(char[] separator, StringSplitOptions options)% W; ~$ U1 c6 q6 d0 m8 j4 r
& N: d. F. ~+ J5 O# X
程序代码/ F9 I* d* }/ j: {( a2 }& W
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素: L$ e3 v! m" _! f' E
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
, M2 X4 k( O F: n$ ^: v
8 c5 F2 C4 ^$ j( k+ b- x) C4. public string[] Split(string[] separator, StringSplitOptions options)
: U Z r' {- I1 i z+ `, y% q! N: d- h4 r8 z
程序代码! \5 y. h; V/ w! a
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素& L' D0 `- y" q! n/ j
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* y/ \* f' L' g3 O
0 g& G: l% o# w5 q! R8 p. c5 z5. public string[] Split(char[] separator, int count, StringSplitOptions options)* K, M g9 A) G, e3 [8 R# r; u5 H
9 s8 Y' ~4 T- h/ o6 u5 R8 H
程序代码
, u& t6 R) ^2 p3 m& p! k8 J, hstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 t V5 c5 |/ w& p* O* F! tstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素1 }; m6 e, ]' @
; m7 G7 ?3 x2 p# y+ ?6. public string[] Split(string[] separator, int count, StringSplitOptions options)
3 ^& {& ]* |7 B [, }
2 F0 b3 f3 O9 l( a代码! Y4 I. I- c/ v7 E' a V& n
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素$ }# [; ]% q0 j. _: H8 S/ b* U
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |