下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看8 U8 X! T# [! Z( p7 ^1 n" p
http://www.itwis.com/html/net/c/20100506/8234.html
7 ~! n3 Z8 F) w9 C3 z8 ]" z4 H8 ^# @% i4 w
程序代码% \0 F5 A$ k) m( ^8 o- c
1) public string[] Split(params char[] separator)6 f: y; X: S, K, G3 H1 e
2) public string[] Split(char[] separator, int count)8 |- H \2 d( L2 R( M6 H9 D# z
3) public string[] Split(char[] separator, StringSplitOptions options)$ O/ v) J+ @" V7 C
4) public string[] Split(string[] separator, StringSplitOptions options)" {1 K6 Q) j" v/ E1 H% w
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
9 }3 f$ p! T" G" j$ }6) public string[] Split(string[] separator, int count, StringSplitOptions options)
7 _( m$ h. {4 n5 H/ M( N
8 i/ n6 f$ X- I8 U* p下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
: u. O! y3 E) u6 m n1 p% s! F# A5 c) ~
1. public string[] Split(params char[] separator)$ X" h0 l4 S* M% L: w8 |
n' }7 n: G- F `% P5 i h
程序代码0 o# Y% r8 Y- |8 B H
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
0 M* G) k( Y& vstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}) n+ G( K# B- Z0 n" Q2 ^
' l1 |8 z; q3 w3 S) X- D6 _
2. public string[] Split(char[] separator, int count). a: i5 I" D: S: g
/ I; n8 d& F6 F
程序代码9 n! L& Y: ] `* K+ u
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
0 b( Q0 U; G' ~: w3 n$ i- vstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}7 O+ d# G: Y' [8 E
! E5 ~5 J" b' U& d3 L+ b3. public string[] Split(char[] separator, StringSplitOptions options)% j8 v$ P5 D: b% \. M- |
! O4 \& W8 m# W. L0 S' r 程序代码
# N4 e) [) ^4 n" L% i6 z( P% H5 Tstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
; v# t; e' v$ o" R! U# _string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素$ O3 m! @& e5 R$ {* G; c, L
# \2 i$ l' P. ~7 v$ w
4. public string[] Split(string[] separator, StringSplitOptions options)
+ ]8 [6 y/ O# O# m6 F* s* [
; C( t$ ~* s5 P- c 程序代码
; {+ J. n4 R" S5 y2 B* q1 h% ?string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素; u5 G0 H o: w
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
, V* ?1 f" R2 d6 z* b- e, g
" d% s. p9 [% q7 T" P) z8 `5. public string[] Split(char[] separator, int count, StringSplitOptions options)' d' ]1 J) p: @* @0 N
8 U$ Y4 S3 b3 j' h. y 程序代码
/ W+ F0 l' ]* Z$ g. ] [9 a0 kstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
Q! U% b7 y$ |2 j6 Y1 Zstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 d, Y, F1 c W" m3 N. e: B5 z7 U
% c3 \( J6 m, e/ k( Y/ K; v0 a3 k6. public string[] Split(string[] separator, int count, StringSplitOptions options)
+ ?, [1 \8 L2 P3 Z+ B2 t+ Z7 g) P8 `: a6 ?/ S' n$ I
代码& w7 D3 }! ~9 r$ w# A
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
' n1 \2 W2 B5 w) n* ]/ istring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |