下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
6 i- W6 l& a9 \5 Phttp://www.itwis.com/html/net/c/20100506/8234.html: T2 s1 i+ S' n$ m' c8 M1 |( n
! P/ h- m8 e! e" R! r8 G' n程序代码
3 \* U) q3 F3 F5 g0 L1) public string[] Split(params char[] separator)
$ G/ i" d9 G; y7 L- |- a) m9 `$ C2) public string[] Split(char[] separator, int count)' E$ k/ B, h7 ?5 D: v! y1 n
3) public string[] Split(char[] separator, StringSplitOptions options)
( `% @! s& ^8 D' A" M5 H- f6 `5 H( U4) public string[] Split(string[] separator, StringSplitOptions options)8 `' b* B5 Y! O. i* ?) P4 w, l
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
( F& D4 j( C8 D6) public string[] Split(string[] separator, int count, StringSplitOptions options)* w, u' u" L) L. I. ^& H1 @5 W
" U/ Q$ @% }- ~2 {& p2 H+ j# n下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):$ O' Z- w4 i8 ^ V
6 g8 `( D5 G4 C
1. public string[] Split(params char[] separator)
V; r5 T/ h# q% n% e* u* c$ O2 ^4 C/ {5 i
程序代码
2 v; b/ J" {/ v' Y+ ]$ n1 H3 nstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
( B8 \' _* J& ?8 pstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}8 B% r ?0 B" C
$ J/ L/ F( p2 |0 n7 n2. public string[] Split(char[] separator, int count)
- t* Y/ w$ L5 G4 z X5 P( _: b# [7 I- p' L! p8 Q' D( Y: ^
程序代码- I* h% @# E! v7 t
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}. U; r* k$ ]- w4 ~- `. | B- J
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
$ e- ~9 [9 a q5 P! H( ~' D* Q/ {; [! u" b3 q& Z$ N/ J
3. public string[] Split(char[] separator, StringSplitOptions options)$ T0 Q) a) E3 d& M( R! | Q
) ~8 L6 W0 |) q9 h# y' w$ T 程序代码
* T9 Z4 F5 _' W* T4 d4 I% Z! _# ^# E$ tstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素! b+ b& C, B( P9 @( j( K
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ l' M! @: x# O+ ~0 {# S q: j
' h8 O$ a! W+ ~% q: l( k, e4. public string[] Split(string[] separator, StringSplitOptions options)/ {& x. n8 f" P* i& a
' {- E5 ~/ Y) H# q* _ 程序代码7 ~( B& \2 J. H( ^/ k" D! I
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 r, N* |& g+ u Q$ Z- L6 ?
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! y7 ?3 A0 {5 [( E: B8 y
; w4 j( B! W- h& V5. public string[] Split(char[] separator, int count, StringSplitOptions options)3 h* T# j; n% }3 |7 N) m( Y
/ m/ [& h) Z# I4 Y 程序代码
& L* k& X; C$ w' x5 fstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
8 S- T! F7 q) n6 f# W0 v$ \string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 K/ N9 K' q9 |( D" [4 M: h% g
`8 ~+ z2 ~% K0 z8 Y4 r. i6. public string[] Split(string[] separator, int count, StringSplitOptions options)
, L9 C8 L) d3 R2 @; u# b, H' X( e* F; i3 f: Z
代码 C+ v. V6 |5 _% ^! _' s7 s
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
( `+ N' e2 E9 Q- i( X* Mstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |