下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看$ M {0 h. i% I8 ?# x+ J! ]
http://www.itwis.com/html/net/c/20100506/8234.html2 S9 b! B5 S! k" e9 b( {: ` U5 o
9 a c# A2 O8 V. |6 V
程序代码) N' t8 u; d! a# u' W$ Q
1) public string[] Split(params char[] separator)" F. ^1 ^, ?8 D& z# _' R w- y$ b
2) public string[] Split(char[] separator, int count)
* f: C+ D5 k: {$ ~) t* W: q5 G3) public string[] Split(char[] separator, StringSplitOptions options)
" z& @2 w' {6 x6 a; x. ~$ r4) public string[] Split(string[] separator, StringSplitOptions options)1 E1 @; \& m. Z
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
y9 r6 T& Q! S3 ^6) public string[] Split(string[] separator, int count, StringSplitOptions options)
! P1 G# R4 }) w0 O9 R F1 N$ J# p' F w5 e, B" b4 U2 {: v
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
* c, W1 P% E1 C" y1 @$ e" u1 ?- l2 i& m3 B6 S# [
1. public string[] Split(params char[] separator)( Z' i7 K+ [, f7 U0 {. _
4 n+ P' g* s. F- S3 L
程序代码
5 J& P, X' G: h8 S) `string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 V3 S. h( [+ L
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}' `) @. k8 ?; }9 W. I/ ]$ w' c9 \
: Z8 n& l3 _; G0 H% H; P9 A# F, i7 P2. public string[] Split(char[] separator, int count)
6 |/ E& `$ d8 k9 Y, e, [6 X) r
, S* J4 Y2 f) ~' F 程序代码
% O1 K m( `, t2 l" F: B& Y( c9 j4 ystring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}+ U. L0 [* z5 t
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
% \: z( c9 X {( ]$ C
- ~ ^& q4 s' H- |) D! k9 Y3. public string[] Split(char[] separator, StringSplitOptions options)+ R4 j! Z$ f3 O! X) }1 o
- v; J& D2 ^7 k4 a2 N 程序代码
" q$ e/ ?$ g* j5 [6 U: S4 Gstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
# v6 [* }, {$ k6 j p+ B6 H3 ?string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5 Z/ t, y4 U8 c# n! B, V
, u, I. e5 \" ]* v+ b. h6 {4. public string[] Split(string[] separator, StringSplitOptions options)0 p1 }- Z- }1 |9 t5 V5 Y+ R3 |# G
1 Q d8 \$ V( H8 ^ 程序代码
2 A. L& J. Z) }. xstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素: ]. U% c9 X: S1 L, g
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素) ^# h5 j" d, X d4 y. i8 w
/ s1 L2 d% c# u) @$ [4 l5. public string[] Split(char[] separator, int count, StringSplitOptions options)
- p9 T2 j0 a! n( K7 G0 ]% m; U2 @7 s! m
程序代码
* o. W+ r" |- ~' W5 t; c1 D ^string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素$ S" F7 l; c, Z
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 P# e" K# @; E0 d, H0 d5 C, z" Z
& _. s, x/ m7 w; p. t% |1 a6. public string[] Split(string[] separator, int count, StringSplitOptions options)3 b: t! G ~& }/ }+ \5 `4 K
, j) n/ `" ]. ^2 z3 w代码
& n$ D2 `; _- s+ gstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
" M, p7 G. l9 B" t% g+ Ustring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |