下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
i9 g5 q5 S+ a K! r0 hhttp://www.itwis.com/html/net/c/20100506/8234.html
1 w h! ]( \: i0 [2 o( w2 J$ E5 ~: [0 X
程序代码
+ k4 g: n$ J* ~( d5 Z$ `4 [1) public string[] Split(params char[] separator)1 C; e, V3 p& h4 o/ Q
2) public string[] Split(char[] separator, int count)
+ O6 U6 [: u! a+ s4 w3) public string[] Split(char[] separator, StringSplitOptions options): Q9 v, y0 c/ C! K5 C+ v
4) public string[] Split(string[] separator, StringSplitOptions options)" X/ ? R$ X# J
5) public string[] Split(char[] separator, int count, StringSplitOptions options)* {& k$ k5 ^8 c6 O% n
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
+ ` ?( c9 e0 t W8 {3 J$ H, T Q. t3 q
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
R/ {6 l, \: m: L4 r+ m3 j. j2 \ \3 U6 G. Q! i# j
1. public string[] Split(params char[] separator)
# m& u& m4 D& h: |9 m0 T# p- @* q8 |2 \# m( p0 f' p; z, M
程序代码
4 t& g9 m* W* Q% E! |$ @string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}- T2 @" I3 r0 F. {& G I
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}/ i( ?+ ^0 k5 \5 D) O
+ T; K: e( ?- M2. public string[] Split(char[] separator, int count)+ R$ A9 H6 X' Z) y; @- ]8 n9 N
% P; N+ a& O# ]$ R$ s1 y
程序代码# o6 {/ r( k7 M; \+ f2 J
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}4 n! y% r& U0 z0 L1 }
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}5 Y; S5 L$ D0 Z/ Z6 _9 F4 S9 D
% x5 ]1 f6 Q; C% _* ~8 C2 e: ]* x
3. public string[] Split(char[] separator, StringSplitOptions options)3 b3 _3 q. @6 [3 [5 X% H' O8 S# D
5 H5 G9 |) q7 a* X 程序代码4 Q4 x- z5 Q; L7 N* U# J d8 g
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
: ^. `4 U. s2 Q9 }. z# c, R+ wstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% g3 j* U0 o; W" R0 W9 m6 Z
- K; [$ F9 C- |! C7 a4. public string[] Split(string[] separator, StringSplitOptions options)% a5 e" {/ V8 I
9 G' T" x4 q: k* N 程序代码
* [+ m4 Q9 |8 Ustring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
& T$ e0 Y& |) i" pstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 j4 \* ?+ D0 e7 d; ?( b u- Q" i+ ?
5. public string[] Split(char[] separator, int count, StringSplitOptions options)$ E/ G! E% G, O
% O9 q- }; N3 d6 K* }8 G
程序代码
, P7 ?4 R3 d$ nstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素. y- S/ k7 S4 x
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' d1 @8 X2 Q" P% r
! Q, t7 [/ t5 A8 P5 e6. public string[] Split(string[] separator, int count, StringSplitOptions options)0 l% L) p( e; w, Y L! B
$ Z5 R/ i$ C k7 Q代码
: A9 @9 q* ]3 \string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素) F8 c5 C* n; V* E+ h
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |