下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看7 ^ m3 b; B0 E$ @
http://www.itwis.com/html/net/c/20100506/8234.html
& c9 @$ ]- e) f$ `0 ^5 s7 {! h6 M' S9 }+ v
程序代码
; n, N3 e4 V# q+ T4 Q1) public string[] Split(params char[] separator)1 q6 Q# I: m7 L
2) public string[] Split(char[] separator, int count). Y. T1 N% i% g" o$ @" z, w( ~" U
3) public string[] Split(char[] separator, StringSplitOptions options)
9 X1 e# f1 p$ G, N' }% l' v4) public string[] Split(string[] separator, StringSplitOptions options), S: Q0 U" _( v) P
5) public string[] Split(char[] separator, int count, StringSplitOptions options)7 M# V7 }3 Y7 Q: J, P- s* V
6) public string[] Split(string[] separator, int count, StringSplitOptions options)- d2 i8 }; Z3 y2 ^. l' l
2 i' ?& A# K; l2 ~# x
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):; n8 N$ Z1 K& @9 Y
4 v g! n( O- P7 w
1. public string[] Split(params char[] separator)8 v+ z. T) n8 b5 W4 K! {
8 [$ u+ W6 e' ?$ F' W- Q' ]
程序代码, {' u. I- \) ], {) ?
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}. J5 ~8 I5 j; O3 c& [/ U# L4 ~& }
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}) r, a6 I" u$ d. u# T( L- A
: x g1 F5 G' C$ ~. `* }/ j
2. public string[] Split(char[] separator, int count)
# Z0 x! @$ Y- J$ L! t
4 s4 c9 l0 \ `0 d 程序代码
4 L2 B: y1 c9 X( T7 J7 O! bstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}; s& I' u3 N7 V9 y4 o" B
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
7 X, l( V0 {8 l1 q/ S. s* e9 E3 d( L- h: a" q+ ?8 V
3. public string[] Split(char[] separator, StringSplitOptions options)1 Z5 d' L) Z/ C8 x* L+ B
0 v9 Y/ E7 Z0 n7 {$ h
程序代码' e. f# |2 y( C, e# j, X
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
7 F7 p! G( I. b6 G/ V9 Qstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素( _# P8 M4 M' J# S; H7 P
) }* \* _% l/ v5 R' h) s' z
4. public string[] Split(string[] separator, StringSplitOptions options)
5 r7 p- ^) B& Z; p9 t/ t! `% I: e4 y1 s3 `9 \, T" v/ t' k+ U
程序代码8 e! t m9 N0 P' F r( h
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素/ }! C/ B! I/ z! {2 j$ M
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, I9 R1 D8 C" e4 v( L
& \7 G2 S1 O- O9 L2 O- }- c. T* |# `# K1 p
5. public string[] Split(char[] separator, int count, StringSplitOptions options)9 r% t( l$ X ~# ~! j/ `( Z e& d
) j U* g+ E; S 程序代码
1 [4 }7 C, i9 ?. x- q$ sstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 Q6 ]1 j4 B1 H) Gstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* Y2 ]7 Y0 N: D% i; K: D$ h6 a* {
! E J- ^$ |9 ^+ K0 q" N" r# n1 \6. public string[] Split(string[] separator, int count, StringSplitOptions options)
! i* V" _! L9 r- q: w! X
' \% k) I* j- N" l! v- u代码' @4 r4 _; e- P _
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素; d7 o0 g8 {1 U: J0 ~
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |