下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看/ \0 f4 v' }, z u: g+ L8 E
http://www.itwis.com/html/net/c/20100506/8234.html
( {) ^; G$ p7 h1 b6 `6 D' }+ p9 e. H) {" o6 J
程序代码
2 p& t. z5 p3 }5 Q; T! m' R8 e1) public string[] Split(params char[] separator)
6 C; B( K' d/ ~0 _* P2) public string[] Split(char[] separator, int count)
# O5 ?! s' F0 Q7 v% k3) public string[] Split(char[] separator, StringSplitOptions options)) c3 _0 ^0 X' {) W+ ^
4) public string[] Split(string[] separator, StringSplitOptions options)
; {$ [- r! e; A1 T. P% H* o5) public string[] Split(char[] separator, int count, StringSplitOptions options)$ B) G6 D& i6 k- R
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
" i% C4 V3 g0 X3 r% ]5 c( U1 s
/ t- U% c& @6 I& d下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):" K; L! l P" z& G$ y5 J
2 N Y. R1 j) l! n& n
1. public string[] Split(params char[] separator); }2 X1 g- D* Y
4 j7 I: _8 {6 u* V( P; [
程序代码
0 J5 h$ c( d. m; c6 `8 X$ ystring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
$ g% f" l# r. [1 n) U4 V+ C. m4 ]8 Sstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
M1 s: X) d y7 J/ s3 R7 a
2 D- }' A6 D" f9 }2. public string[] Split(char[] separator, int count)
! j6 e R) ?, R" S
6 E& N8 R8 `. ~, D9 d* B8 c 程序代码' N3 |3 k9 M' t7 ?2 H' O7 |
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}8 m7 M2 X' _& H( q1 p
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}$ Z+ V/ ~; U0 S% C! x8 R
1 C, r: p% U2 V) v3. public string[] Split(char[] separator, StringSplitOptions options)& U( g, c- C/ G& {
8 T: B' t3 M7 J
程序代码6 P$ L# r b* U0 e v# [5 P
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
$ N/ v% V d) }3 b' f8 n& Y9 N7 C/ e( Wstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素" W d: h' P+ `
" E ~$ g( E: l; {' |
4. public string[] Split(string[] separator, StringSplitOptions options)
0 P. u4 s! G# v [ M7 F+ H
/ q" f/ R4 B: X3 p 程序代码4 Y% ]" ^0 ]) o: i7 q0 _
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素$ C' p- D4 p. u9 v
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: V8 d0 @9 T. i1 O1 A- `% }3 g. F. U- ]
5. public string[] Split(char[] separator, int count, StringSplitOptions options), ^8 \6 L' l* c! I" b
8 y) t! v3 B0 D1 G5 H- X( I; V6 R 程序代码+ ~ S' g' D4 _: {
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素0 N5 [0 E, d# L( P( M
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# R9 Z7 @ |. p; n
2 h) U4 g! e* c4 h6 S
6. public string[] Split(string[] separator, int count, StringSplitOptions options)9 n/ ]0 N$ X6 g: ? g$ p
$ [: U) y5 A6 ` C+ }, h; R
代码
% ~- X* `+ E) [/ Bstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
( r# n" k% V$ D \/ w9 ~5 [string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |