下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看) p; {- Z1 \; M# G& B7 R+ H
http://www.itwis.com/html/net/c/20100506/8234.html; E' `( h: b6 u1 k0 G
8 M0 E' L, @, u3 c/ k# ?/ R' T
程序代码* i5 r3 S! b0 Y6 ~8 ?; s" O
1) public string[] Split(params char[] separator)
/ G* E; }1 J: G2) public string[] Split(char[] separator, int count)5 L$ s L% e# m; F7 K% |
3) public string[] Split(char[] separator, StringSplitOptions options)5 v* h l$ @# {) N' F1 h
4) public string[] Split(string[] separator, StringSplitOptions options)" W+ q3 Q4 W3 Z9 o6 V3 l
5) public string[] Split(char[] separator, int count, StringSplitOptions options), K) n3 ]; A/ r# u' I
6) public string[] Split(string[] separator, int count, StringSplitOptions options): { V: }1 P, p/ L$ e
/ }5 m! Y, s9 }1 L5 z# Z
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
% Q, a0 i# V V8 L+ b$ p
6 u5 j" y1 i. z1 }! q4 [: R1. public string[] Split(params char[] separator)
5 M. o/ g$ F% Q
, x8 @" \! j+ U 程序代码
4 b; C" g# y# `1 p6 {string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
( j+ a ?3 L0 d, v$ wstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}% k4 H3 H1 C4 e% h$ q
O* A; G; X! G& N8 j# b5 X* o& P0 L3 K2. public string[] Split(char[] separator, int count)' l5 `9 j9 K: L e$ q
6 z' r1 Z0 e( z, m" U# u 程序代码
6 G8 x" |, i9 @ t0 }string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}% _/ q2 g k* b
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}2 [3 r% v4 b' L( M/ t' F" U
% V' n- ~$ Y( V7 b' e3. public string[] Split(char[] separator, StringSplitOptions options)- m5 S/ M J6 U' h; Q4 U: i
; [: s) }/ j3 }: P2 R4 j* V
程序代码% A( W3 g& H O" N! @5 I
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素9 A# M- [( k& d K+ g& b0 V+ l
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素% z; X& H& h2 E, M3 `2 u7 I' S
* _5 ?- L# x4 x' C) D' U, V
4. public string[] Split(string[] separator, StringSplitOptions options)6 D, g4 k3 Z$ t! j
; o1 @$ z" l8 I 程序代码1 u& z i% a& T+ b6 ?
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
! T/ V0 ?4 J7 l1 Ustring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
- R0 {8 y& n8 J. x, H9 E
+ c, Q0 N' K0 c9 n7 o& ~$ p5. public string[] Split(char[] separator, int count, StringSplitOptions options)7 _/ n! k6 S/ P" t0 _1 r
7 Q7 I# B e7 V7 K+ `& r- T1 B# a1 J" m 程序代码- v+ L: m: ^, |& i
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素0 \* E6 _* o; Q* _" X: h2 Z
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: |' s6 Q+ X3 P2 g" b( t! R
. C( K* j. P$ d6. public string[] Split(string[] separator, int count, StringSplitOptions options)
/ w- d# X" n; }( ?! e+ `; V, ]( E* ]5 N8 P2 x7 y
代码# s2 k0 g3 D% s
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素6 E# m4 }& Q$ |& r4 T/ i9 x
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |