下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
9 ]! t% A3 C( e& p1 i1 m+ _http://www.itwis.com/html/net/c/20100506/8234.html
5 L* k% }; Z7 C6 O2 O
! B- e& B0 Y. q- x! W5 b' i( e程序代码/ W2 _! M+ m4 [6 h
1) public string[] Split(params char[] separator)
. t6 v) f4 ^, H! x6 d2) public string[] Split(char[] separator, int count)
# S1 G. }8 L" H3) public string[] Split(char[] separator, StringSplitOptions options): A# R* B1 C; Z& u+ x9 z( e% H# X
4) public string[] Split(string[] separator, StringSplitOptions options)
5 o. b1 @* S2 p/ a8 {) ^( t5) public string[] Split(char[] separator, int count, StringSplitOptions options)) r+ C" t! C1 I5 @ b+ ^9 @
6) public string[] Split(string[] separator, int count, StringSplitOptions options)" ~7 @6 r* n3 E
|% r: h% Q& H! |& ?$ E# t下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
. I$ T/ r0 g2 w; a& c: v) h6 S9 r+ O2 ~( g) Q0 W5 B& n( F
1. public string[] Split(params char[] separator); q' \# N) `& y& e
4 U7 o0 {4 n- I; ?! X% j4 W! R# x6 E
程序代码5 }3 k1 G* E4 D
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
; o% S* w' Y( xstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
' q5 _7 |4 h r$ [! @. Y6 O: ?" Y! y: N. U
2. public string[] Split(char[] separator, int count)( u. e( n) ~( n, Y. O ?9 p
" {% J& t. d* \$ T( {, n 程序代码9 a m- Q) [$ W! w5 @1 v
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}1 \% I0 P8 k) l" s
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}0 ~8 x0 B8 y" i0 Y1 K: S5 I8 z! t
7 E- a# \, y; Y9 A7 g1 V
3. public string[] Split(char[] separator, StringSplitOptions options)
$ C/ u( x$ \' O3 d
( @6 \+ I. c" F% d9 y. a( { 程序代码# G1 z1 E; @8 m/ P) q/ b
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 V4 x+ T* y" v
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素8 R8 z3 w( [9 ^
4 i* k! P1 ?2 ]& q9 a; Y7 |4 U$ t# T4. public string[] Split(string[] separator, StringSplitOptions options)
9 f4 ~% H' t& l, D# F1 p) _- ]( _0 ~4 P' o M1 A
程序代码
& k1 b! B/ {1 U+ g" @string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
9 t! T( k/ v1 t7 a' W2 u/ m) jstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 Z/ G3 {8 Z0 G$ |) A4 ^
l: t- ]/ p4 G' f( i5 N- y# e5. public string[] Split(char[] separator, int count, StringSplitOptions options)/ u3 Z# F( E! i8 _& K5 I0 I+ {
4 G) L6 g4 P. @* _8 G5 R3 X6 I
程序代码
2 [/ u9 l5 s n) Qstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
+ d( G5 Q2 a; @: Y9 U8 k; astring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素2 x& F3 W, z- k: L1 F9 z' I4 y; L
- F& y) }9 a; ^/ H6. public string[] Split(string[] separator, int count, StringSplitOptions options)$ ?/ a: e/ r! A' @
) I6 l6 W$ j9 K: {7 b# W* T- _代码" p6 Q" F* p( T
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素* O0 U9 w, x: v, ]
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |