下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
5 F h9 t: ?9 U K* ~$ D$ S, f& Hhttp://www.itwis.com/html/net/c/20100506/8234.html9 v" b- ]( W& |6 m: v S# f7 _
& z+ S4 h& w2 x* \
程序代码4 u3 V. T4 e' X# L! `, q) Q# @; A& }
1) public string[] Split(params char[] separator)
( |) ` y6 F, \. |9 u9 a2) public string[] Split(char[] separator, int count): V+ e# A2 D! A1 u
3) public string[] Split(char[] separator, StringSplitOptions options)2 V# l& W0 R4 u
4) public string[] Split(string[] separator, StringSplitOptions options)6 T/ u2 l6 R" ]$ N
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
+ Y2 ~7 r4 m( k0 a5 F7 Q6) public string[] Split(string[] separator, int count, StringSplitOptions options)
8 {; k0 H) L# Z+ ~8 H
- g; `$ F& d8 ?' y& z k' Q下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):. z3 B: ]9 ?3 o+ J: t4 g
( F F7 s! ~9 b$ I3 |. F1. public string[] Split(params char[] separator)
6 _& J' T+ G" ~# Z9 f+ z) S
6 n* w1 x* X N% y% h/ h 程序代码% P9 Z7 s2 {* o; O. o3 _4 X# c
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}) j- O- M: B* l9 X! c8 |/ k
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}3 P8 [0 f- B6 T: Y6 ?
5 N0 l0 j3 m) C( a5 a5 q
2. public string[] Split(char[] separator, int count)* v+ E& ]# a5 Q9 e7 c9 n; `8 l( R
8 @5 s- P0 z9 I! ]+ A* W 程序代码' P0 @+ l8 `& K3 \0 P- N* g
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}" K' u6 v4 q7 N* @8 \0 |7 S+ J
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}; d5 |0 r* p2 I
9 r9 f) N' a3 D1 l. G3 j3. public string[] Split(char[] separator, StringSplitOptions options)
7 s. a& \2 V% v: Y1 v
. m% f5 T0 r1 w6 [) i6 H- N8 r 程序代码
# o* l, K& x0 e8 ~9 q$ _string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素+ a2 M; V( B( o0 ~0 ?
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素0 z* d' j) c* e1 M2 V `. ~: e
y9 ?% }" z3 \
4. public string[] Split(string[] separator, StringSplitOptions options)
/ v2 H9 M) h4 _2 u. r" g2 r3 |# Z8 ^3 m& r% a4 f
程序代码
" x! G5 M9 L* y2 {string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素3 W) _9 `( Y8 [
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
& g1 X* I8 `6 V1 z$ [, Y: w. b) h, w
5. public string[] Split(char[] separator, int count, StringSplitOptions options)3 v0 _/ f* N; H& h, x
' F R" T# z0 t; Q 程序代码
) i& _1 E; l. B3 j+ hstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素. W1 ], T- I! |; t& w
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素4 R O' q" ?4 g0 s
0 H$ Q, Y2 i1 @" I: ]
6. public string[] Split(string[] separator, int count, StringSplitOptions options) }# {* ?) o/ ^' t# j! s% ~7 v
$ h3 u8 t6 E- \4 Z代码5 G; _( C' ]) ]. F4 o! Z! i) d9 K
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# Y- C1 o! z; j, o+ Z" D* Fstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |