下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
$ f: w8 O+ i) v" a5 Fhttp://www.itwis.com/html/net/c/20100506/8234.html
& w7 O8 A0 w2 u1 L0 ^5 C! ]5 T1 V+ L$ [
程序代码
$ R8 f" Q2 i7 g/ {6 }1) public string[] Split(params char[] separator)0 \) }0 O; f! y
2) public string[] Split(char[] separator, int count), O6 J$ ?' q4 V8 b0 z4 f
3) public string[] Split(char[] separator, StringSplitOptions options)' r" w" z/ [4 ?) W# r2 i4 X6 B8 x
4) public string[] Split(string[] separator, StringSplitOptions options)7 u. G4 G, ?# H* f: O4 k! u
5) public string[] Split(char[] separator, int count, StringSplitOptions options) [" w& s3 f8 n& X V3 F2 f
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
" v7 [3 R9 B' Z8 k0 x
$ x* X2 ]! ?# G) l8 p下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):# r9 ?5 c& c V' H P7 q' ~. w
% h o: _5 ~1 E! ]" o. J
1. public string[] Split(params char[] separator)2 Z& F3 P2 _1 r! Q2 Q9 t8 x( u8 t8 ]
2 C) D4 @) n+ K B) m8 T1 ~! g$ f
程序代码
$ ?! b& X6 \. {+ t! q0 Z3 tstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
4 L* h# g/ O. Q; j, K5 ^string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
9 U+ [# h! k5 D& `0 v3 G7 g6 o+ d
2. public string[] Split(char[] separator, int count)
2 B; O5 @/ i6 [8 p- G# n. T( h% z T& G1 n3 [3 _. ^
程序代码. }& g, S2 n. A* [ U) q# u
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
1 l7 i; p' ~( t7 Q) cstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}# M V# y9 V5 ~
$ @4 s1 L- N. s3. public string[] Split(char[] separator, StringSplitOptions options)6 K2 s5 j" {, s6 H' ?2 D
+ V% t/ x2 H5 L0 K 程序代码( U& {, K! `: k, F6 T
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
# z- R! Y: E! _1 ?6 x4 Ustring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* \" E7 Q& ~- A, e* s
' Q1 R! {8 H+ G' W4. public string[] Split(string[] separator, StringSplitOptions options)0 h$ j+ n3 W: I
0 Q2 |3 E6 T$ q! U5 a5 _& D
程序代码
0 ^; Z5 D0 l8 }7 H# l/ ^0 \string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
" ~/ R- ~4 k1 p- h/ e5 z8 A0 ]string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素% t" j* a* R7 c# f
1 e! M0 }/ y- M* z5. public string[] Split(char[] separator, int count, StringSplitOptions options)$ v* ^7 Y' x4 H# T
6 @& U# W7 b% E( h& M+ f$ Z/ G 程序代码/ G% o b& Z' E5 E5 V8 b. O
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素3 b7 g1 Z8 d* G9 d" {( j$ W* ^7 B
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
/ [- @ X/ M+ K/ X( A5 o+ H- R
. d3 H, S4 ^1 F5 t/ x. `6. public string[] Split(string[] separator, int count, StringSplitOptions options)2 B: w' K l4 P% \* _% U0 _
5 u4 u) b- {/ }3 Y1 @; b* `
代码' k @5 N2 H+ w. G) S
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素5 E2 w9 Q4 N b, n0 h
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |