下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
; V$ V, w7 M1 X1 ~http://www.itwis.com/html/net/c/20100506/8234.html
* Z9 f% W0 H7 p1 s9 m2 N- D
) \4 |' w, u+ B9 J. n程序代码
) q) _" ?' C0 q+ G0 ^+ S: ~1) public string[] Split(params char[] separator)' `0 b1 M6 b Y! i4 O
2) public string[] Split(char[] separator, int count)
3 E, t) ]& D4 e3) public string[] Split(char[] separator, StringSplitOptions options)" H$ V( A( ^2 j* D8 U* \
4) public string[] Split(string[] separator, StringSplitOptions options)) j/ l+ u3 O$ C2 r
5) public string[] Split(char[] separator, int count, StringSplitOptions options)$ W3 d5 H4 C* T
6) public string[] Split(string[] separator, int count, StringSplitOptions options)! J* ]6 g; y8 e
1 o( T* e0 {9 Q" J. C+ B4 b. Y2 X下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
$ }( W5 Z; T; h% P/ N# J; n7 g
7 q3 q/ Z; o R1 A7 u7 H* F1. public string[] Split(params char[] separator): G$ E" A5 K4 w6 k" Y
. f* [0 m, X3 m! O! h 程序代码) r& n$ A3 O- z2 V4 Z. D) \2 W
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}( ~6 X" ]; s7 ]! [" R- G0 d3 B
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
& J5 p) M9 @! O, Z; _; _, Y1 s& ]( Z( |
2. public string[] Split(char[] separator, int count)% ^! N/ B; e+ v4 C7 I9 G2 l! r* c
1 w9 {! \3 C4 [/ Q8 `# W, h/ m 程序代码
7 G3 _3 r( Z/ u& e" a+ dstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
$ u- M0 h6 B3 R9 ^2 i8 r3 n) ]- }string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}# H* { x; Y5 }% Z
8 e- E9 s, I' {/ z* z5 V W
3. public string[] Split(char[] separator, StringSplitOptions options)( k5 m2 Y' B& c# W
) m# l) L, h+ F0 J. [+ }
程序代码 d0 p( m6 f, w0 o. B8 m
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素! B5 j$ f4 x# D7 G/ ~4 [: n) b
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# Q. i& z# e/ ^3 Z8 M3 o& F' D
1 ~# @: i6 A& Z6 S4. public string[] Split(string[] separator, StringSplitOptions options)
; \ @) X% X% L% p, T4 c; q# v! s, u8 Z( f5 m: U: H- I
程序代码
6 t: c r9 S) Y% p$ H: a7 {2 }string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素6 |9 z/ a1 I. U! H4 C! c" t S
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素( v. E! X [5 ]# K
, p% S0 e, h! X [" |5. public string[] Split(char[] separator, int count, StringSplitOptions options)
% I6 z7 O# E0 o& s3 K% I" d/ D% ~5 @0 W. Q* E2 o
程序代码/ t! ^ S% N$ f+ u# e* _- }
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素4 k4 V; C y4 W; D1 L0 @
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素3 R: e% s* A1 p
7 | [/ M. O- l& O2 ~5 v
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
) _- @% ?* @3 Y8 P3 u( t4 |; g: C. I9 F
代码; |/ G9 r' k; f3 x! V9 H
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
: O( n- Q' _: g/ u, ~string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |