下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看, s7 }/ c4 B/ c( L
http://www.itwis.com/html/net/c/20100506/8234.html" U* @, }' R1 U4 ]
* X/ ?/ W% q2 j: U% \; x6 H程序代码
" i" F" ?% e3 r% C H4 s1) public string[] Split(params char[] separator)9 O9 z3 a$ l$ q3 ?' C4 `& v
2) public string[] Split(char[] separator, int count)# N( ], j, f3 p. y8 {( A
3) public string[] Split(char[] separator, StringSplitOptions options)
/ V: o" z9 B) j3 y4 x7 {4) public string[] Split(string[] separator, StringSplitOptions options)
0 C: n* t, J8 N8 m6 {6 {) y6 D5) public string[] Split(char[] separator, int count, StringSplitOptions options)! K! }9 M1 c& y8 Y7 ~( m
6) public string[] Split(string[] separator, int count, StringSplitOptions options), ~, Q5 G' l& y& H
, Q% k0 b0 [' e1 I
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
, a4 V# B0 z; P, m3 m# g7 M1 p4 D) }( M1 Q; K k
1. public string[] Split(params char[] separator)5 R8 |, q ]* w7 `; ?9 ~7 I
; ^+ J$ j( e3 M. W- q 程序代码% u: V' ^1 M5 Y1 p
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
1 C8 k! n: D0 T. J0 s: p7 r! b: Nstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
: J% G6 d% X9 I, D- `
! |; v5 A; z% ]0 E& \2. public string[] Split(char[] separator, int count)
! }/ q; [5 W5 M, j9 P) H
8 u5 R( b9 K$ D 程序代码
2 h/ o* T3 @! e5 c4 s7 istring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
& ?, J3 d* y5 B3 _6 C( Pstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
8 c3 }# ?5 K7 g
# a. n) G9 K1 x" `, y3. public string[] Split(char[] separator, StringSplitOptions options)
; k" _4 Z- g4 |, q9 D# s5 K0 R9 g, C" T6 c
程序代码
# @. J, X' [$ o3 Z% P, Q+ H' Nstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素' C5 k. g- K+ e
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
! V, H0 q, v% x3 n" V+ S" A+ f6 U0 a+ u. w; w* A% e- _7 ]& h# N2 |
4. public string[] Split(string[] separator, StringSplitOptions options)) N4 d% |4 n$ Y
9 [' r6 M* i: P; e/ y. _/ H
程序代码
- l% z) _& o7 h" j( P# ?4 s+ pstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素8 c; h" f/ }6 k( y; x$ {7 w$ A; z
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
9 ]# G, w. j6 n/ j2 A* ^; ]3 g/ f" T- J! F* ]
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
: t2 k$ P" C* ^5 E4 e Y: X2 J# I+ |4 f! k
程序代码
# R! \# O. z' g. Pstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
4 b0 ]9 ]( q7 o& {string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 ]' m9 P% P& ^
( ` S0 c/ }: ~3 a1 X Y, O6. public string[] Split(string[] separator, int count, StringSplitOptions options)
# Y0 V# T' m$ o+ \4 l, _, G6 u {) J/ |9 f3 `
代码
7 f U* m4 a: n2 G. F' hstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
! _& p% S; |& q( d y& z- u& |string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |