下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看6 D* b9 q) F: o5 A& d1 s* z
http://www.itwis.com/html/net/c/20100506/8234.html
; r2 y* b5 C) K2 _! N( E9 r1 |: f' }5 L4 _& S$ M+ r; b' N/ @
程序代码 }; m# K/ l X% Q; Y
1) public string[] Split(params char[] separator)
2 O. i$ T' l. z: V2) public string[] Split(char[] separator, int count)% S- t% X+ ~# `" [- R
3) public string[] Split(char[] separator, StringSplitOptions options)# w- Y4 z0 D. a( {# J! ?
4) public string[] Split(string[] separator, StringSplitOptions options): u Q5 Y5 k+ N1 {( ?
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
# F! M t0 w6 q6) public string[] Split(string[] separator, int count, StringSplitOptions options)
% j* ~8 f( x& Z: g X, H+ I2 V6 J1 o
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";): a& U- }7 D" U5 C* L/ Q& L
0 K3 q \6 R9 Z5 s; [
1. public string[] Split(params char[] separator)# k: }; N g+ l! ~0 l3 N+ e+ d& |
- J8 E. X3 S# ?! R 程序代码
, K' t8 N+ e! j) Lstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}0 q# e. Y3 @& I+ w6 ?
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
4 X+ z7 ]# {6 [# M' `2 c5 u
/ S( X$ i3 H6 I, M6 u2. public string[] Split(char[] separator, int count)( r& u/ I# p4 M* M2 b/ ? J1 u
+ B7 Z6 w, d8 S; Q: a: N' D" C
程序代码
7 x% @- _% m8 C/ h5 `string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}1 ~+ j! E; z: a; S0 i
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
3 T _, |& w' |& M* |' h) ]3 T4 S, {+ M4 Y
3. public string[] Split(char[] separator, StringSplitOptions options): S% C2 Z+ H5 p5 D9 {& B- k# Y0 a! Z
1 x; g e' E7 w O4 G3 l y 程序代码
; ~. g3 e* }6 b3 S, z, w& xstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
6 F+ Y' Y1 t5 ]9 P; Lstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素( b) j- Q- i0 E* ?+ _! M& ~- L
1 Y/ h4 u5 y' p9 `, |( q6 x, `
4. public string[] Split(string[] separator, StringSplitOptions options)2 @/ g( V0 L0 ^) N4 Q/ Y
% V& x( G8 X3 ~/ a' Q) v 程序代码" F- j* |" q# |( t+ k' o
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# z9 C/ {% K/ L
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 ^5 V0 @1 k/ J# @' ?3 f L
; P7 W2 f7 z: A, V6 @6 w5 M5. public string[] Split(char[] separator, int count, StringSplitOptions options)
# A* h0 p! `8 i5 ?, B) ]! D% t( u, K4 G( o) [% P1 r
程序代码
. Y6 r7 l: X5 l5 sstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素% O* E8 y% N4 p1 m" w( @$ e7 V1 b
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
& e. z! R' x8 J* r/ c
8 b7 v" m% T+ t$ D- \; ]* y/ J6. public string[] Split(string[] separator, int count, StringSplitOptions options)6 u2 P! J7 N4 {+ f8 \6 E% U" A/ `
( m5 }$ H# L" P8 ^" o) m" K
代码
: M. Z+ M6 K0 ~string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
0 @! p$ y, w8 M0 m7 |! nstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |