下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
5 Z( C- H0 ~: b1 a' e8 E* N, y: lhttp://www.itwis.com/html/net/c/20100506/8234.html3 s! k+ |- k# b+ u) @
+ q2 b8 {; s! }4 q- O) h
程序代码( F( F; b- i4 t
1) public string[] Split(params char[] separator)
+ v. t5 O" q2 p. |3 o" n7 b/ F) h2) public string[] Split(char[] separator, int count)
2 g3 S3 R0 K2 B# \. ]3) public string[] Split(char[] separator, StringSplitOptions options)0 l( J, S) n$ t: X$ }& a3 v% q
4) public string[] Split(string[] separator, StringSplitOptions options)+ N( b$ _/ g+ i- g: K0 Q. j
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
9 ^! a1 a% g3 ]1 W# X* D/ O6) public string[] Split(string[] separator, int count, StringSplitOptions options)) ~* Z# o/ E. n! _3 q1 ]
, u2 R+ z) p& s3 n- ^' Z
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* c ]. B0 k8 f* [% W; i8 W: o
, J, l/ d3 _4 m! v# R {3 Z
1. public string[] Split(params char[] separator)! j; s; K- c$ Y1 }4 M
- A) G5 L7 V# F2 c! X; n 程序代码/ k! C3 r4 I" s* J4 Z
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}$ F, q0 s/ g! T; m
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
1 v0 t; t2 Z/ o0 h5 w
4 f& _3 h+ }9 J# t# q% b, w9 E2. public string[] Split(char[] separator, int count)% @: \+ X3 T+ F$ c2 f6 ?( e3 v @
, Y% q9 r4 H0 {0 T8 x$ r+ ]
程序代码
4 i1 P% W7 x7 y8 y) Pstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
7 `& X7 I8 q7 hstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
; x, A9 X4 x& \3 h1 @
+ k- M% K' Y4 f$ r3. public string[] Split(char[] separator, StringSplitOptions options) f. Y, y2 I6 y7 C
% N. x+ x/ o5 T8 w% Z
程序代码
( ^' M2 z( M* U) qstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素9 ?& N0 V- R6 x# m
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# p# o# Y# Y1 s
. \6 o) O$ ^7 ?4. public string[] Split(string[] separator, StringSplitOptions options)
: {5 y) x1 R; I6 W5 U! _- |! T9 U& O7 c0 R/ |; F: ~
程序代码
4 h9 O0 s( ?5 @8 `string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
/ E( w. l4 j m; \string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
) I) C: A# Q% d2 C' n) I
5 e" ]) G+ p# c, r9 {5. public string[] Split(char[] separator, int count, StringSplitOptions options)
) i4 f3 T. w/ I: l7 J- l0 T
6 O2 \% R5 A7 u& K 程序代码
- {6 a" J+ T7 {+ m* J, Hstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素& O- b. L* W; t3 y8 d! ]( { u
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
N3 U# P/ D7 z9 s. v! s3 {) X$ s4 @+ R2 I' J0 F
6. public string[] Split(string[] separator, int count, StringSplitOptions options)3 r" o! z* p. b$ G. j9 G
8 _; z7 x. g: x; O1 x' |2 D& ?* x
代码 r2 n/ ]' L5 x6 F* r7 L8 `
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素1 C# u4 `7 m# k4 ~& _1 I
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |