下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
+ s5 q* y, {1 o* V6 y$ `* whttp://www.itwis.com/html/net/c/20100506/8234.html+ v3 A9 }+ |8 M0 d
9 ~2 }- u$ ~8 d- t8 O0 y0 B程序代码, A7 ~% n. S7 T0 x
1) public string[] Split(params char[] separator)8 N+ d; t5 U- {) ?& X
2) public string[] Split(char[] separator, int count)
/ y& m6 v7 J& Y3) public string[] Split(char[] separator, StringSplitOptions options), k1 M( y$ U1 S
4) public string[] Split(string[] separator, StringSplitOptions options)
|. K j, E# k: r' E( [7 M) i5) public string[] Split(char[] separator, int count, StringSplitOptions options)
! F1 D q1 ~+ p/ A3 s) y& m( `5 Z6) public string[] Split(string[] separator, int count, StringSplitOptions options)
0 @2 J2 j/ w' c$ |- ~0 t8 S0 y& n7 d) K; h: y, f+ E# E
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):6 @9 Y2 ~! K& r1 d$ P7 l8 R/ _1 \
, Z$ {4 E7 l( g
1. public string[] Split(params char[] separator)
, j0 Q# r+ N! E# g( w) B- H: J7 n8 |1 i$ x2 F# x' g
程序代码
8 c+ P7 z% h" N4 {/ Gstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
- l+ I1 E- B& x) t! t5 Kstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
! p( y w1 u9 B9 Q' w o
/ |- S% |. A0 ?. Y5 ]: K2. public string[] Split(char[] separator, int count)/ ~# f6 h& F- H9 g
9 l2 z# J- M( ]6 @- V 程序代码2 r* x" \* J1 z! d9 }: }
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
1 w# X7 c7 S( d; X, J3 vstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}# `/ e4 d; I' S# S: ^
+ ^5 L% [& V6 u$ e* }3 b
3. public string[] Split(char[] separator, StringSplitOptions options)7 P8 B) c" _& ?) K
3 P5 Y3 b/ @0 X. Z& ? 程序代码: p5 [8 \9 h3 }1 J& j! e
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素1 i, b- n& m+ k D' H4 s
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
+ ^4 k: c+ z7 F8 f& ^, w, M
* d9 [ b) A- ~1 Q+ l0 @' a4. public string[] Split(string[] separator, StringSplitOptions options)' H8 ]- T8 W! q! z4 P
3 Z2 W ~! ]' Y- }- }% K
程序代码3 C9 {7 ~; @# X+ `6 h
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素: Q2 H( x4 E( V3 J1 q. p
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% x. }, J @% H5 L8 B' F
$ ^! y8 d/ T/ t5. public string[] Split(char[] separator, int count, StringSplitOptions options)! U7 c6 Z) v: l5 G; ~
7 c( j% |/ ?) ], c0 M; i
程序代码4 w& K: t" H6 ~, Y7 [
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
! [# @8 u2 i, Estring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
4 c- o" Z( I/ t( o7 M
0 o# M: ]2 l" M6. public string[] Split(string[] separator, int count, StringSplitOptions options)1 T. u: s, C( m' `( u) u
9 J, Q( I, x1 |% v" e. v代码
, w u7 [, g% d. N" t. ^' e2 q% t1 sstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
" k& x" i$ u; X3 W3 {4 U! O3 M* n6 |string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |