下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看& z+ z0 `$ G9 `- D* z9 }- G
http://www.itwis.com/html/net/c/20100506/8234.html
* v9 R" E# |- |/ H$ j( r: V! o/ J* F$ m# f# V* q
程序代码
+ l M' I0 q1 B! F7 y; p5 y8 {1) public string[] Split(params char[] separator)
: l' j9 N/ f1 u, M2) public string[] Split(char[] separator, int count)
1 t+ ]% ^' m8 n3) public string[] Split(char[] separator, StringSplitOptions options)
, w& ]6 r6 S2 L q8 h4) public string[] Split(string[] separator, StringSplitOptions options)
& _2 R' a! q% U2 F O. J5) public string[] Split(char[] separator, int count, StringSplitOptions options)
' e1 i" L: L U. D. ^6) public string[] Split(string[] separator, int count, StringSplitOptions options)4 O% x8 S1 j$ i! k6 a
* n: ], [# @/ Y/ a下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):, m1 Y5 }3 F; C6 r% P
, F* _7 w- q, Z' M( |
1. public string[] Split(params char[] separator)
) H# M* h9 `7 V5 P) H2 R' s
: E* s9 E- N. N% g0 f 程序代码
4 V" t# J' }8 z, Hstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
9 [' |1 Q* F& X1 |string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}' i. b7 {9 Y" h
2 u7 I; ^) ~" E- S& `# {2. public string[] Split(char[] separator, int count): s& ~+ i4 N5 U9 |
3 S+ W5 c- W: Q" F( `9 i8 V1 L" o
程序代码8 H! G6 V+ p1 g/ ]; J# h. p
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}* H9 T S9 k5 `( G/ [5 T
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
; t2 {0 m: d& Y( @
7 g1 K, p2 @) z( u1 H3. public string[] Split(char[] separator, StringSplitOptions options)0 X1 Q0 S" K$ x/ U& `
. D9 W3 A+ c7 e9 q) r) i 程序代码7 `' ~* o# K/ W/ A
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素& m* b% m) F% e6 @
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素9 o" }& B5 \% a# }
. H7 v" `0 {3 _4. public string[] Split(string[] separator, StringSplitOptions options)
! a* A9 m! N8 l2 P+ `/ F% Q: p! p0 W' Y2 v' M' R& ^# T
程序代码
Z1 F0 m: w7 n! e0 j* j+ ~( Xstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素! L$ S% r5 W) U5 m5 a3 y1 o- i
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5 ]& l; D* d- \& t( ^4 G/ w0 W$ G. O' w1 a# v. Z
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
3 ~6 s. w+ R: R3 B3 f1 |, O5 b; y: b( P, ]+ }
程序代码
n+ u' M" N/ c" h5 _; C: C3 U: Ustring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素9 n0 O- |1 ~% \
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 X% V2 {0 r* Q6 o7 l$ y/ E
. ~( g8 `1 n {' ~/ B5 Z) x
6. public string[] Split(string[] separator, int count, StringSplitOptions options); `8 x* L7 f9 h. P. [$ b( u
5 w3 S' D9 d& }' B1 r代码
5 D; E1 T" c* C8 L& G7 f- Ostring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素# ?3 G* p6 d# _, d( c: f, j
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |