下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
$ f- k# O* h: @4 Y6 M7 [/ Z9 [http://www.itwis.com/html/net/c/20100506/8234.html s0 O% K$ a: l5 l
3 C) H7 b& w6 V; }; O9 K, T2 ~" K程序代码+ d8 W8 [) L) U* W/ r# k2 v
1) public string[] Split(params char[] separator)
8 w) f" C3 v' m* A: C2) public string[] Split(char[] separator, int count)
1 v. ~9 V+ }2 X. O3) public string[] Split(char[] separator, StringSplitOptions options); r# ^6 T7 G' `: }+ ?
4) public string[] Split(string[] separator, StringSplitOptions options)
2 w9 C" Z! @" d+ y5) public string[] Split(char[] separator, int count, StringSplitOptions options)
2 J$ M O$ C( F* C/ H) j/ ?6) public string[] Split(string[] separator, int count, StringSplitOptions options)
) n" q3 `& ]; e9 k! R
0 b1 D7 A. k/ K, F8 |6 k下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):. C3 H4 ? O1 L
' {5 Z: }! A5 D. s2 N, Q9 a- I1. public string[] Split(params char[] separator)
* d y( p; d$ R+ C/ }! `- a9 i+ N! [' u% ~) B$ m$ i* n; _
程序代码
* u; T |( o: I( fstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
! x) d) |% V' k: N2 Sstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
! P: j: R. d1 ]+ v$ V4 g4 b( g9 h% {9 T: g; g" I9 E# s
2. public string[] Split(char[] separator, int count) u$ X+ u3 ~; j/ f
9 M- k( h4 I$ l/ ?' f( n9 J, _ 程序代码
5 C) s& b7 O! ^: q @8 {# vstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
0 S$ T( i& j* e2 O( Q) a; ?string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
) ~. l+ t! A# F7 n0 G* `" Q- s% u( _4 ]2 R
3. public string[] Split(char[] separator, StringSplitOptions options)
4 V0 K4 T' m4 G. [4 q( p
6 ~5 w/ M1 w( k/ T 程序代码
/ K7 a6 g( l/ W3 ^( `* k4 L+ R( Lstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
' @4 K# }( _: C2 x- F/ J! Qstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6 M+ w9 f- \5 X& n7 o
. Y) z3 I& w) a M& q; r1 l4. public string[] Split(string[] separator, StringSplitOptions options)
7 |9 f, p: y, `* K
/ T2 ?/ `7 Z6 v4 f( n0 o" r 程序代码* C# l5 o) q4 d/ i/ m
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
/ a+ t4 l. A/ `: j& q9 j2 _string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 ~6 L$ s) |) c7 O# F4 Y/ h* f
, q+ W# M0 l2 K- m( N
5. public string[] Split(char[] separator, int count, StringSplitOptions options)! u& p+ v$ U- @3 |; M
* x* a% m+ y" D# ^$ ~- w u- I 程序代码, ?$ n; l% h: V) v; L: v+ W8 a
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
1 m2 J8 h. c: b0 U$ P& v+ a- d6 `string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素& D, j$ R; L( E8 F% F
) C `2 I6 @) ~6. public string[] Split(string[] separator, int count, StringSplitOptions options)
$ T6 @& k7 C. x d9 E( o3 t8 B
0 O( v5 Y- j, ~3 a2 P0 T. J$ j代码
- O* Y+ I9 H" q9 w5 ~) _# ostring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
3 w* d2 q8 L8 |" n; | Rstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |