下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看0 ?2 B! ~0 L' r: @
http://www.itwis.com/html/net/c/20100506/8234.html
6 S/ s9 p7 J0 z
; I2 M3 b( H- V程序代码
% X4 K, v! W( v. }6 x+ X1) public string[] Split(params char[] separator)
4 r, F. C% ]' H9 g# F7 f5 w* c2) public string[] Split(char[] separator, int count)7 ?7 w& f' R2 A/ K2 E6 C
3) public string[] Split(char[] separator, StringSplitOptions options)
* h/ T. ^" s8 e" Z# f; w Y4) public string[] Split(string[] separator, StringSplitOptions options)( D* {1 h1 B. S8 e5 }
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
! B. c. ~9 P* ]2 h4 f; j6) public string[] Split(string[] separator, int count, StringSplitOptions options)* ^, B& n; k" t! A9 |% q
, b+ z T. } Q# T' ^) Z! |
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):: ~8 g9 B Q: O4 C' |
0 Q( p+ q$ `6 G& A9 c6 N1. public string[] Split(params char[] separator)
9 S2 p- m: b5 R$ {5 w1 f5 j- n& N
' t8 z+ [: { c7 ~9 c! \6 {3 C' U 程序代码; O* H/ l5 ?9 j3 I: d, p$ m
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}) I: e, H& O# n8 s: {+ U4 \
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
! ?* b7 Y" W% Z0 [" V" M: W5 T5 I: y% I& C6 ?
2. public string[] Split(char[] separator, int count)
0 {" z% Z7 B2 E$ D1 E) {- B6 O1 ?1 i# G! O
程序代码& S$ g* C* x9 [6 q, `# m2 o
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}# I; L0 ~8 K, u7 F3 d
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}" C( I4 C0 B, N1 A5 F3 x4 [# b
9 E$ @3 m y7 J, X* \2 s8 x3. public string[] Split(char[] separator, StringSplitOptions options)
$ D% [* J1 `/ u5 o* K6 P; {7 B! D0 D' O: o6 j9 q
程序代码5 b0 \9 a5 _* n6 g
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
5 n9 f' `1 Q! z) Vstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, k) a5 i0 b, Q5 D5 k% @
( v3 ~. q2 k; D4. public string[] Split(string[] separator, StringSplitOptions options)
+ `2 K R, ~2 |) {. c: V' O- H1 I4 C$ F: ~4 F! T
程序代码
' e G+ E' B. [2 Xstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* W2 l! d: `$ ~( j
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
1 A/ j( Z O8 V. {0 @2 K
+ c) U1 a T) e; F; g5. public string[] Split(char[] separator, int count, StringSplitOptions options)- l( F9 M3 ?9 w. c8 f. W2 z" L6 Q( X
& X |1 c% |; x& h8 g0 L# @8 h: E, J 程序代码
5 d/ ~: F1 s! n; Cstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素+ M! m/ x2 j: k
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素2 J: @" L# E) U5 `7 O
$ _- g. a6 J$ `
6. public string[] Split(string[] separator, int count, StringSplitOptions options)- h- O) B9 W0 f
" s# {. M3 P/ C
代码
* ^* ^' q& Q+ w( bstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素% J% W, P- _% w. J* i
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |