下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
& M4 x+ {' P2 L- ohttp://www.itwis.com/html/net/c/20100506/8234.html. q- i$ W* y6 a, `
# w( {- q. K* _% J2 p程序代码
# F8 M0 u- R' z, G( @' {% y9 I1) public string[] Split(params char[] separator)
+ ^$ Y. }5 \; s( @, l: u# g4 H2) public string[] Split(char[] separator, int count)" d: G' l8 u3 y. B
3) public string[] Split(char[] separator, StringSplitOptions options). {2 G) c& ]5 O R f
4) public string[] Split(string[] separator, StringSplitOptions options)
/ Q2 a2 I. l6 C; L6 ^5) public string[] Split(char[] separator, int count, StringSplitOptions options)
3 U6 z' a% D7 e0 ?! F6) public string[] Split(string[] separator, int count, StringSplitOptions options)
! ]( D0 A. p6 ]5 B: R6 T9 U# W3 o. t: @; k/ S( Q
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
2 F9 q* f, Q, K: e1 G2 L7 L4 c' Y# C: g& E
1. public string[] Split(params char[] separator)8 e! |% i* V, q
# I% A, ~% ?# d9 [! L2 k( U 程序代码
8 g! D2 ]! m# m2 gstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
# R, X- Z9 S. s e5 @0 b# Kstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
: D2 _; K3 p6 j- W& q! y5 Z7 F1 p/ {$ E
2. public string[] Split(char[] separator, int count)
& `- g& N- V' E- x+ [
7 d2 u, b* R; w 程序代码0 j; t: l- e3 e. D' n8 m" p. G
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}8 ? ?# s" G+ }8 c9 x
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}% y) }% C$ Q" y
% B* w& h3 @+ ?
3. public string[] Split(char[] separator, StringSplitOptions options)
% e2 ^6 T) }# N, F5 c! _* R; a& S9 n+ E6 @0 {+ p, i& `
程序代码
( l' J* m! W6 J& Z- }) I, f2 Fstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
& R6 m* P' q5 q% mstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素" v4 t! j) k4 N. k
; v! u6 A% S! `2 L2 A
4. public string[] Split(string[] separator, StringSplitOptions options)! m4 Z2 Z0 V+ @, e
4 T1 B, e: d& ~& j 程序代码
1 g( o8 ~& q; W7 D. @string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素% R/ J/ _: I3 V* q* b
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( u; {! h7 @# C0 C2 J/ v6 Y' Q- O9 {5 P, K4 y( |
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
+ d0 \/ Q/ `) r q. ]
4 x% K: z% y' o( [+ D. [, @/ a 程序代码
2 G6 L0 {5 y% U3 S% k; Sstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
8 R6 y' U$ d/ E* a( zstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
# {3 d/ h- e& Z. ~. S. j, ]/ w6 P! K
6. public string[] Split(string[] separator, int count, StringSplitOptions options)9 p0 Y$ E% W, N8 g) v Z
. f2 e+ ^0 J+ e8 ]* [
代码: |% ~! I0 E8 |3 Z7 c
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
5 b4 H. V% {* n2 E! K2 M6 Rstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |