下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
: v& m8 C7 k) [9 Y$ phttp://www.itwis.com/html/net/c/20100506/8234.html
5 ?, s6 E- M, |' H: p
- S2 M+ ?: a6 {程序代码/ `" W# M6 J& Q7 F- \) B o& ~1 }
1) public string[] Split(params char[] separator)3 T* ` P8 g( X- V4 h/ ]$ d; ?6 w, ]
2) public string[] Split(char[] separator, int count)3 N& K7 b9 I- P1 L/ A6 Y. R0 \
3) public string[] Split(char[] separator, StringSplitOptions options)
7 l% I& x0 ^$ k/ F9 ?* W) [4) public string[] Split(string[] separator, StringSplitOptions options)3 f. [$ Q& r! X) R( Q3 o' w8 s
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
+ \5 W a, x8 v, Z# s7 u6) public string[] Split(string[] separator, int count, StringSplitOptions options)- \& c5 r+ q" {7 D8 j* c" M
/ d% c) j$ Z, ^" Y1 |
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* u3 ]/ U6 i# ]3 o) u
9 C2 ^6 P( |6 _4 v( f# D1. public string[] Split(params char[] separator)
/ L& C1 t4 Q; Y/ l9 b6 `) a! H
, |( w0 J2 ]( o+ w( ^8 F( l 程序代码3 t& `7 l3 Z. C* t2 J* R; K: h* f
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}3 M1 J' O- L5 G
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"} Q v2 h3 {. }# y7 M
9 n6 i1 w( u7 W- B) N
2. public string[] Split(char[] separator, int count)! `# k6 I" v, W1 c
7 D: c# Y# C- C! C 程序代码. J4 j3 z- S* q0 S9 w5 c
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}4 ^" i9 B! B5 \. X4 e
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}+ P: D. J/ }2 _. z
) w4 I' C2 V% V- G
3. public string[] Split(char[] separator, StringSplitOptions options)& D; ]5 k( ]5 L) N9 v
$ ^! b) B; G& A5 v 程序代码9 O6 P* C* g/ T- ~! y( i
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素; Q. [" ]2 A+ z0 Q: e# L( z
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% m9 c. s9 j g) Z0 v/ k8 E5 X
2 F$ B) c5 m( @5 V, J$ s7 @! ^4. public string[] Split(string[] separator, StringSplitOptions options)
4 t: q8 r0 G$ A# z2 |: I0 J
3 D0 ]+ X& e9 O8 y3 ?! t* v7 d 程序代码
4 f$ k; [! p) R. E) k! p( Tstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# A# b* y( r9 m1 A6 g0 r
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 I' P. c' g7 T% ]- Z( X: ^6 p+ K% M( L1 K
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
4 w5 {- I7 T4 v" F$ I# o
. T( s3 `8 r! W6 Z, @ 程序代码
; t, o9 m9 b* p. Lstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
) S9 O! S1 q6 e; y* H Fstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素& `# x1 M* o- i4 ?' z9 x
: S e, i. y+ W n* Y2 b8 f
6. public string[] Split(string[] separator, int count, StringSplitOptions options); [) f. o' O3 r7 g+ o8 Q, e- _- Q& r
1 W ^; S$ X$ P9 e# k
代码' a% B! K& p1 p: U" n( {
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 P0 q9 Q0 Z! k0 E: [% ?$ gstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |