下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
: g) F# Y7 e# |& J" [/ r4 ]8 Whttp://www.itwis.com/html/net/c/20100506/8234.html$ ?: W" J8 a& h3 o. N) S
, [ G1 n( m" C+ }: m% v8 _4 g
程序代码
u4 ?3 h' E6 G8 h( p+ \1) public string[] Split(params char[] separator)6 T, y; x7 _0 r$ \3 @
2) public string[] Split(char[] separator, int count)
! h! D6 d& N4 X9 A, o3) public string[] Split(char[] separator, StringSplitOptions options)/ y; M9 z2 }0 ^ m. D" G
4) public string[] Split(string[] separator, StringSplitOptions options)8 r! O2 q8 B* o2 a4 W$ G
5) public string[] Split(char[] separator, int count, StringSplitOptions options)/ e: Y% [; x) A: L+ i1 ^
6) public string[] Split(string[] separator, int count, StringSplitOptions options), m4 e$ [. \5 X0 A7 ?8 {1 O# a2 ~
% ?# j6 f8 y8 @' I5 d下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):4 \, T$ W6 m9 M0 ~* T
) e: ]' _5 A1 O! m
1. public string[] Split(params char[] separator)9 f' N% @9 i& I) W
+ F& }8 z8 z4 F8 ^) T$ D, P$ i( R" f0 t 程序代码
V/ t6 o4 g Lstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
8 ]% ]% i" ^% _% zstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
: E) W( X# T( L" X
( l9 F+ g1 i" I/ p$ |3 A1 i2. public string[] Split(char[] separator, int count)
, a& Y2 k9 ^% k0 e" {# @8 s: @# f
8 i2 Q3 c6 K$ o, O 程序代码
+ L1 s' e3 Z1 F8 Z* l lstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
' W1 q7 @3 Y+ h$ |5 [/ t1 dstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
* T& I' x8 J) ?* K4 H, n' ?) l: }/ a- m( Y
3. public string[] Split(char[] separator, StringSplitOptions options)
# C) s3 ?9 m1 N' A* c! p/ [& [, B. `+ V! J
程序代码
( Z- {8 W# K" W2 A) ^2 Gstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素: Y8 [! g) }" X( M5 ?
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
1 y1 A: S2 L$ I( i# A$ M: d+ B& E# [7 H2 [4 I
4. public string[] Split(string[] separator, StringSplitOptions options)
/ _$ A4 n# k" N* E! L3 w, e& k u. ^3 a* } }
程序代码9 Y( P! _# }$ {
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素. M* i3 c, [% U) x! d6 m3 x
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素' G# F8 _. |% X5 f, B
4 |3 \- {( X2 v, F( ]4 M
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
8 r$ _6 d& p4 {1 }) ]3 U {
$ k; |( q& d' r( E/ H1 X! u7 L 程序代码2 g9 ~$ o& ?4 F% Y5 F- ?2 @
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
2 j' Z$ E+ p2 ^9 L8 N5 J9 Tstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
& G& Q- Z' O9 F, E' G/ U9 f6 d) u- |1 ?; @4 [
6. public string[] Split(string[] separator, int count, StringSplitOptions options)& H/ @; }. I- ?: V# @- H3 n" q
! V1 k- |. Q1 C; j: r7 Q- q+ y代码: k4 Y" x" u$ O) n: ^# F1 f" Q
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素6 D: Z4 J/ I; V4 L
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |