下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
4 `2 O" |6 Y2 Y4 Mhttp://www.itwis.com/html/net/c/20100506/8234.html6 I9 j( k3 X! e: [6 R8 a
0 W$ E1 O1 _' T
程序代码) A1 {# I9 e- V) l/ `5 v* H: r) s
1) public string[] Split(params char[] separator)1 P0 y( ]1 t1 y4 h. x
2) public string[] Split(char[] separator, int count)
3 H. N- u. h! U( j3) public string[] Split(char[] separator, StringSplitOptions options)
( i9 z/ e: |2 T9 a4) public string[] Split(string[] separator, StringSplitOptions options)9 v8 `" c, B* U p
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
" a* @) z8 E, \0 ^7 I6) public string[] Split(string[] separator, int count, StringSplitOptions options)
+ b O( |( E6 Q% h6 a4 v5 ?! l8 K8 ]. J* @9 U3 X* C
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
( t% m3 r D( \, S! f
- t* K# d1 X& ]5 x' v1. public string[] Split(params char[] separator); V7 X/ L m7 a+ \
- o3 M2 B$ ~$ }" Q' w0 o
程序代码
: J. ]# j% _! n1 Z* Lstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
0 J& S+ j x' J, Zstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}( q! N& V2 {' H4 E# o7 t n
0 ^& e1 ?. a7 g' T2. public string[] Split(char[] separator, int count)
% }1 u C$ q' Q+ I9 \& g' d. _; h2 a) l/ a# K
程序代码
6 G0 Y' U# j* H0 T! ] E$ G: C# Pstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}/ S3 J4 E* E% f5 E
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
" r; T& ]% ^+ Y, y) O: A Q z A
" Q* r7 s7 ^# f f5 P; [2 z3. public string[] Split(char[] separator, StringSplitOptions options)9 Y M1 h8 t$ `+ e( k4 ?" w* C
( R _0 e, a. w5 ^* Q7 M* x2 k
程序代码
) V" U9 o r5 T z7 h) S+ H+ qstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素* W# D2 q! Y* R! N
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ w) z- ]( | W' e5 \
0 Q8 A- Q; A: ^% I! ]5 v
4. public string[] Split(string[] separator, StringSplitOptions options)
, {- T0 s1 J4 }3 {( |: f* X7 f9 a5 E# O4 a( D0 z- M; h
程序代码
: ?; | y7 w* T _# M1 _string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
9 b- ~' K& z, Y/ I6 `! B4 ^string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素0 q& |, t+ i/ c- c/ i
! w5 v i* N3 H) d, O5. public string[] Split(char[] separator, int count, StringSplitOptions options)
' E5 v3 O: j/ }( ~" h; a7 k
4 W: C1 Z! E# n1 C/ G0 D# b: G 程序代码
! ^9 e3 u3 o7 \# @ [+ gstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素- Y3 t7 F* l$ Z' N2 p; I, P
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素3 c ^* \- O/ I$ x+ B
: ^& D# p1 M8 x6. public string[] Split(string[] separator, int count, StringSplitOptions options)
" B, z( r) @$ G
( Q, G5 M3 X* g2 V/ Y! ] o8 \代码# o& ^6 E, V0 }" M. [1 c* u
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素& h9 }" g/ U9 ]5 f0 p0 e% x- N. m
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |