下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
1 s/ _* t7 o, B" g, E' Q+ mhttp://www.itwis.com/html/net/c/20100506/8234.html
2 ^2 F; i) ]+ d: P$ J" t$ k
! R+ }4 {+ ]7 t* o& Y' Z程序代码. k, l F) B" n
1) public string[] Split(params char[] separator)
/ o- {7 w! ~, m3 v$ w4 x& R2) public string[] Split(char[] separator, int count)5 \+ _" y" j7 }. u8 D* v
3) public string[] Split(char[] separator, StringSplitOptions options)" l4 k3 I+ z. B. P/ Z, ]+ L
4) public string[] Split(string[] separator, StringSplitOptions options)/ X3 v: R* g, ?( g6 h0 k+ R* \( w
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
- y/ E1 s: _5 G6) public string[] Split(string[] separator, int count, StringSplitOptions options)
6 Z" z7 s$ N+ [) @& O3 j8 t8 t2 N* [
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* e! f3 D% X$ g3 G6 W$ i
. F% A1 e% n# c2 B8 b* t1. public string[] Split(params char[] separator)8 j, X5 i% L6 X/ {# i
# `9 Z r* C5 f( ~6 T/ F& Y 程序代码0 l9 q$ S$ Y. T, L% Y9 K0 h A. F
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
- X) x9 |+ F/ K3 h0 x# B* Q. Astring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
1 E A0 m3 @! E2 C% Z# f
; b, A9 t, d. e" k2. public string[] Split(char[] separator, int count)
& U, O$ G! w" W
# `' y. S1 B) c/ c5 ?5 { 程序代码
6 b4 B, y" \. Xstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
% e# F3 X4 X7 Q6 p* Y. p& Z2 Rstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
1 H O7 w7 v a! i* {+ f, W
" S0 s: j5 T8 R% r. k3. public string[] Split(char[] separator, StringSplitOptions options)6 X% [6 q. T% A2 B" t- f1 E
8 p7 ]2 \: o4 N 程序代码
+ h8 H- Y8 \7 {, F% ^; Tstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素0 H" [' Q( G! ?; J1 L4 _8 W) A/ H s
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素+ W p. U+ D8 {, ~! |# @
0 v; E* ? [2 J" T2 U4 c8 C9 w
4. public string[] Split(string[] separator, StringSplitOptions options)
H, w2 u5 M5 u- O- H. R) v7 D! R/ h8 `/ x& d& K
程序代码# t* F: `8 h" p: v" K- p
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
& [. j2 V8 `' v4 ]9 J' @- Mstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
& u7 X8 c O0 j( L5 O
! b& G0 M6 m' X0 z; t5. public string[] Split(char[] separator, int count, StringSplitOptions options)* h2 r# H0 P( u1 o1 j6 M
' Y# `6 F- ]. G; h4 Z( G 程序代码
/ [" j/ K9 s F7 Z' X5 Gstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素8 z$ S9 u2 \* E1 U3 u) ?
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素' D& A& _' \# Y
- M; O( X1 q1 N7 l- v, t
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
3 E; a: e3 o5 z* i* m
! s5 Q- [& z9 g$ ?/ A1 x代码; l0 a) J R2 t: ]
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
, U {% v) N, W8 ?7 ^string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |