下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
/ D) L) ^, i6 ^http://www.itwis.com/html/net/c/20100506/8234.html' [5 @: ~" G: u% A6 j
; V3 H* ~( F4 t9 B1 `/ g/ h程序代码* k0 W5 G, Z# D% z9 p
1) public string[] Split(params char[] separator): O5 a0 N0 R2 i0 U; @2 }' y
2) public string[] Split(char[] separator, int count)3 `% k$ q* ^: }( _7 j
3) public string[] Split(char[] separator, StringSplitOptions options)2 B# t( D5 c; ?) @7 c D
4) public string[] Split(string[] separator, StringSplitOptions options) f% N. H; m9 i! ?8 L4 i. f; |
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6 L+ h; E8 B1 a% z$ `6) public string[] Split(string[] separator, int count, StringSplitOptions options)
t6 x5 [! c6 t- X! R( _2 O; u( _6 c! u9 `; r
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
& |) ]' ]/ h7 _* b
8 O8 j" j1 W! Z, Z$ v6 z7 F3 i1. public string[] Split(params char[] separator)
) K, d9 |! l- }) A y2 _* X2 I
- S, b3 Z$ l1 m* S- i# _; T" q 程序代码
: p: P) c) k* k8 i. {string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}: ]5 {* ]% z0 K" k0 R
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}$ L# Y5 P) b- M- e" y3 c
8 d; N8 o8 x; _' m( o5 W
2. public string[] Split(char[] separator, int count)1 p/ E! h8 f. W2 g6 v4 Y1 d- v, O
) j( N" |% `0 w: ?+ } 程序代码
7 P$ i* o0 \* O3 |string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
0 A4 g4 ?9 I! M! Hstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
; Q( V7 r- y# D) H9 j' o! M( N! o" ^! w
3. public string[] Split(char[] separator, StringSplitOptions options)
/ c! I m$ Q$ w4 {8 _; Z
/ b3 v4 A, q) x0 J! G/ _5 ] 程序代码7 y8 d6 z7 V% X3 X" L# ^5 b, X
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
% m/ Q" v- p5 W ]string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素" s% t# d- }- p+ ?
5 i; I9 ^, M6 p& Y4. public string[] Split(string[] separator, StringSplitOptions options)
+ S! }4 \$ K5 n9 w; v% R3 p1 @) Y7 R4 r% I* B( J, G
程序代码. p/ f7 J1 c0 ]7 A. O/ Z
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素4 d+ s# ]! l5 R8 S: }' b; ]9 M
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
+ }, j! G& G; t. W/ B* T& \; v# v* ]- e# M* w" \" @4 m/ A/ d
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
8 m& o) O6 m3 w' U r N$ w- o; b$ Y* r/ |4 L( X. V/ p
程序代码
5 U% j. a7 M4 cstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素$ h, j5 d, o4 y$ N
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 o% K! C' q+ q
9 R+ {! q; e8 c" A6. public string[] Split(string[] separator, int count, StringSplitOptions options)! J s+ G* p, g6 I9 Q; f) _
7 I1 s' W3 z" e5 a T1 \代码
& c1 o N- D3 j5 Tstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
2 ] J0 D1 o% F; \string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |