下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
# G% g2 c9 W; }) J" khttp://www.itwis.com/html/net/c/20100506/8234.html
: k! ? N- Q: p9 p$ d. k: Y; t. B+ }$ e' c m- n# v1 h2 ^
程序代码9 D, Q7 p g* E
1) public string[] Split(params char[] separator)
6 B5 H( W; V+ C! b# g2) public string[] Split(char[] separator, int count)
3 p6 w! O+ y4 _' B1 }7 P* P/ E% b5 y" i3) public string[] Split(char[] separator, StringSplitOptions options)& }5 `4 x- ]* g
4) public string[] Split(string[] separator, StringSplitOptions options)
4 T8 y1 x P* N; M5 F/ x/ a1 {5) public string[] Split(char[] separator, int count, StringSplitOptions options)
8 R+ n- p0 n8 g- `6) public string[] Split(string[] separator, int count, StringSplitOptions options)
) [6 q* p" R( m8 u$ Q
" X* m% w0 ^: C# h7 ?4 v7 F下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
' A+ {9 M8 d, e
, n* b$ [: o+ v1. public string[] Split(params char[] separator)/ s2 A% a& u: v0 `" n& c
2 r3 ?3 F/ k. j
程序代码
8 h% {5 \. P4 N# _- S7 I/ ~string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}: a& d( L/ e! g8 L7 \0 y' j
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"} r& s3 H6 i2 M* }6 d( p9 y8 D4 H! z
; F( L7 k0 n5 E- A7 m! U2. public string[] Split(char[] separator, int count)- D( C( g9 q% H' Q) w. O+ \( B
8 e# e5 i4 b! {! I. A8 G+ J 程序代码
! R4 p- [$ \# Z8 l. m8 G3 }string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}# y& @8 O9 t3 |. x
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}, P% b8 x: i: q% `
1 o& m2 z$ h0 v- r/ U3. public string[] Split(char[] separator, StringSplitOptions options), ]9 f7 w% S8 X9 S$ R# y6 U
4 `, B. m" h }7 ?- [
程序代码7 z- J; G/ V) A" s, m U5 K
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素6 c! x6 g' Q; q$ N
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素" O7 u$ W9 i9 ]/ M
2 o2 D5 u2 U B
4. public string[] Split(string[] separator, StringSplitOptions options)' E5 k: V; L" R$ w, Q
( ]8 T9 C) Y( u7 |( z5 r# v& Q; D* O
程序代码
# \4 B( a7 o7 {+ v6 A0 B3 xstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素, y4 \) I" n7 H9 ]7 _
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- A4 A3 w8 R) Q6 B
- m! C7 `0 W3 `8 `; U+ w
5. public string[] Split(char[] separator, int count, StringSplitOptions options)6 R& A* W6 I6 S
' i+ w) t }1 V
程序代码
K# H! b2 c& _5 M$ m% xstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素; G. s8 q8 b# S" ~: Q+ \
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! h" H, ~! s0 p7 {5 j% b
( v& ]3 u: F4 R: c- @& S+ s7 P& @6. public string[] Split(string[] separator, int count, StringSplitOptions options)8 Q0 @$ t' Y4 B, M
?, P# x' {: }& C- w
代码
% f5 @: ?' O& w, ~& p* ustring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素' x1 Z' b# j' G3 @+ o q; K
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |