下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
5 X8 y! k7 s* _! K4 w( A$ p. ]http://www.itwis.com/html/net/c/20100506/8234.html/ M0 E4 k! F! F" }
% F" Q' R h1 t程序代码
* _* l+ G% Q+ d4 ~' n. Z* P1) public string[] Split(params char[] separator)
7 w4 M1 t) Z# R" d: m2) public string[] Split(char[] separator, int count)/ b3 \9 Q' Z9 q% P' h4 K
3) public string[] Split(char[] separator, StringSplitOptions options)6 u0 N- Y1 c9 E2 u
4) public string[] Split(string[] separator, StringSplitOptions options)
1 \6 V5 b' ^5 S$ J. c5) public string[] Split(char[] separator, int count, StringSplitOptions options)
# U% q( q; e4 [. j- b! b6) public string[] Split(string[] separator, int count, StringSplitOptions options)
) @, U; e4 f0 u: H) F( s+ @3 q4 H' H, h/ A
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
" k! {! r8 W: E* d4 S8 {/ H2 c, M% Q* T* @0 d/ S
1. public string[] Split(params char[] separator)
) i. V, |9 m6 @* o; I
4 Y8 f+ j( w# ]4 L" k! n& i0 J 程序代码* F3 k* e; ~4 p0 ~
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}) ~$ K+ X" {5 `/ Q0 A3 G
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}/ X' n! ]9 p9 K: E' y* ^
: G& f$ R* g1 E! |2. public string[] Split(char[] separator, int count)7 ` Z q( _$ {7 r
$ S: s' u2 s4 j# ?
程序代码
& Y% q) l8 |4 P' hstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
) F7 |/ U5 |4 K6 |& Astring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
3 H* k: B* Y. |
! e1 ~2 z+ Z! ?0 T3. public string[] Split(char[] separator, StringSplitOptions options): E; U G8 i, W( W$ x
6 D$ n; |* y* b) @, H! Y" C
程序代码
: L5 y, ]; @; A0 ~) y1 f! N/ z( kstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
3 X4 I1 p9 Y7 {, L7 _- R' ustring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素# \; J6 x; M+ D$ M8 w8 E
9 q* h1 B1 @4 ~9 Y6 }! ~4. public string[] Split(string[] separator, StringSplitOptions options); y. s0 {- k, D3 y1 m S t, a
/ s+ N( q, Q$ W# V+ S2 @
程序代码$ z& p' t" E# N& B5 Z3 V
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
" g" w4 |. U) {8 fstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- {! E3 K f' P2 B; V& t+ [
8 m# g A- T) ]. b+ P [
5. public string[] Split(char[] separator, int count, StringSplitOptions options)4 A, `0 h M) O; Q: ~
y1 W' |* d3 H0 J9 b& K; l# t0 u 程序代码
6 E1 d! F* H/ J: a- ]0 Y+ G: A, Kstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
! m- D0 P- k! Astring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
$ X& \% B B& T5 P' ^4 L( g* {! T# {9 L0 R: z, S) n
6. public string[] Split(string[] separator, int count, StringSplitOptions options)4 M6 R9 X n2 H7 r" m! o! a
; M/ E1 ?* [# C, b$ \& P
代码
X$ S8 r% v! v! Pstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素4 p- l; f% A! P9 w) k( @- O. J5 C
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |