下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
, h* u5 q% i+ b" S7 qhttp://www.itwis.com/html/net/c/20100506/8234.html8 N' B0 i% v* d8 O
; D( a8 e& l) h1 a, X$ H% I
程序代码
) r* g0 G2 `1 L0 w! T$ H4 i9 I1) public string[] Split(params char[] separator)
3 P+ }- C/ b4 e/ ^2) public string[] Split(char[] separator, int count)3 B" S3 ^) d" ]5 H: f' G
3) public string[] Split(char[] separator, StringSplitOptions options)
; O" }/ y$ G9 n7 @- v5 d% |4 J& m4) public string[] Split(string[] separator, StringSplitOptions options)! @1 L- R% L x7 |4 i3 g; X8 r; m
5) public string[] Split(char[] separator, int count, StringSplitOptions options)* M4 A* V# q. G* z, @8 w1 d! I
6) public string[] Split(string[] separator, int count, StringSplitOptions options) f9 l( L# r$ `% {+ L* A6 M
u: z) x& i& \( m* `: c
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* u! |) N% L: x; R3 q& x6 G
# c. w6 j" X/ i" j& X
1. public string[] Split(params char[] separator)
8 G: h: M) h. G. D! d: @# D+ ~
/ S4 C. X0 B& v: e* G' N 程序代码
8 R4 \9 x) I9 _3 p' Y7 ostring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}- j9 k( d0 q& j$ v# g
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}$ k# O: Y. u. } S2 ~8 r' o
4 T3 }$ b2 h' \# e
2. public string[] Split(char[] separator, int count)- ]5 d3 u& d7 B9 Y9 x& K0 r& s. X
: j* s) _- t* E4 _" u& c# L: c. i 程序代码' {! D6 J2 o5 J/ }6 [+ M8 W/ N
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
$ R& v- @0 h/ j& a: xstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
- v/ |% s9 D1 D, T
( B l5 v) y8 L0 ~3. public string[] Split(char[] separator, StringSplitOptions options)6 C9 h( l/ G2 v4 `% t- z. C6 w: k
9 N" y0 N) F- V# K
程序代码" \) ^2 ]7 }" N3 c8 B
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素( Q$ S9 w& r: n8 J
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 T. M% Y9 r# Z, d+ T3 W
( G) @* e8 m9 F7 l' ^
4. public string[] Split(string[] separator, StringSplitOptions options) R7 {& o' E: g2 j9 ?$ X( Y
3 B# H$ M+ W* ]$ G4 W. M( I1 X 程序代码
4 R9 t% d: y) b7 r& ~* astring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
s0 a/ o6 q0 [! y! I) r/ q( m" x! Tstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 F1 S& T7 ] v+ c
. ~7 D; d- }7 C0 U% z6 Y5. public string[] Split(char[] separator, int count, StringSplitOptions options)2 S V: \* d& d# a+ N* N. ^
" f A+ z5 b6 G
程序代码
, Z. e( ^5 V7 E5 z1 s, dstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
4 B2 m7 @. p' ^, wstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素5 b, ~# s4 w% P
- ~' z% \( t5 t8 d1 m, |6. public string[] Split(string[] separator, int count, StringSplitOptions options) \7 D3 L( J9 j) g. A. Q
& d* A; D$ j9 B5 A! Z9 S8 T. v
代码 a6 [3 l6 t# ]% d% G
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
: q/ ? r4 ^* \8 E1 l' ~string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |