下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看& H" v- ?7 J8 p/ e
http://www.itwis.com/html/net/c/20100506/8234.html& [/ w5 O. O/ x( L: h+ v
$ D3 @2 f8 `5 p8 l o" G程序代码
, [; |% W3 Z/ m5 P- S3 w1) public string[] Split(params char[] separator)6 Q) n( a o# g/ D$ [& D. G- R4 r
2) public string[] Split(char[] separator, int count)
. d; a5 }8 u6 c, A3) public string[] Split(char[] separator, StringSplitOptions options) u0 K* J1 F6 N2 O: L0 E
4) public string[] Split(string[] separator, StringSplitOptions options)
: R) Y) n* `, w& |, |5 o+ {5 I- _5) public string[] Split(char[] separator, int count, StringSplitOptions options)# a+ B8 q) ?3 i& O) r
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
- _* x" w8 T! b: `: X* M( M+ `* i7 h. Q( s" o! g* P; y5 R
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
/ \( D- Y$ H: ?7 C3 \5 q
" N* e% @; f# L# b6 }+ m1. public string[] Split(params char[] separator)
`% K8 P# p5 t7 f, s) q1 S Z. H' }2 K5 Y) A" J1 m! x
程序代码2 z) O% J" K$ C9 _
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}# L# j5 W; m- d* E6 d
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
% B* i+ Y3 N/ e$ w' e4 @2 L
+ B# i. y" L* \; J2 d2. public string[] Split(char[] separator, int count)3 ?9 L& G. d6 k. t+ p+ a* B
5 n. x9 i( c; @! N6 e. ]7 \$ d 程序代码# k' }1 r; G' o2 S* a1 ~% W$ P! t
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}3 {8 L" g8 u5 O( K0 X9 |5 Q$ J
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}" M G, I& Q9 c7 j4 b" Z: ]5 j/ A$ i
" x( X0 v& b! d2 a. ^+ R# H b& B# R7 O3. public string[] Split(char[] separator, StringSplitOptions options)
6 g/ D6 U7 W1 U( ]! n6 t* t
- n) H* w4 Q' F3 q3 A5 u/ t 程序代码
7 ^. ]; J$ G$ m5 T# s, z" `/ |! nstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素0 Q% |1 `0 _4 X ]9 Y K
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: _( D' |* G. B2 F- o+ i* R# h7 O" j1 z/ b0 u
4. public string[] Split(string[] separator, StringSplitOptions options)
5 ]5 g* t* l4 L8 R6 L' M( S# j
, ~+ {( X" J5 C1 K, G v, y 程序代码
2 t9 Z( l- ?3 w4 R! j; ?string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素/ q6 S# A4 k; u2 Q. A
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 h, M' R3 ~9 [& x2 _2 S: h. W, g; N; C7 v5 H9 k7 @, I" e/ b
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
8 Z( S$ h! o" e. n6 L3 C( w& @3 } S+ C, [4 S
程序代码( t% K5 _$ d0 g' s9 e( v+ d) _
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
+ L" b' F9 s. y* r1 B+ v: @string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- d: P/ @) [0 X* R4 `0 O" I8 f
3 l5 }) c7 W! @6 w5 C' g4 |6. public string[] Split(string[] separator, int count, StringSplitOptions options)
, C# S) |' U2 V s, A9 j) E
. ~5 s. L# J9 \* A' N代码
" I9 W4 v. b' a" ~' _string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 s7 |& I) S& g5 }string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |