下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
4 Q# E# ?& P8 P9 bhttp://www.itwis.com/html/net/c/20100506/8234.html
7 c( Y3 p) ]8 D' s F/ j" Z& W3 w/ i3 v1 Q! v+ X) X9 [
程序代码
* u0 n5 r I. N% i- R& `+ D7 v1) public string[] Split(params char[] separator)& t7 |" K X" J* T; J
2) public string[] Split(char[] separator, int count)& F* I! ]$ N2 z1 ~/ X
3) public string[] Split(char[] separator, StringSplitOptions options)
3 h8 q; t* u0 c2 d1 z5 A, k4) public string[] Split(string[] separator, StringSplitOptions options); S1 ^" R' ?: U6 i5 W
5) public string[] Split(char[] separator, int count, StringSplitOptions options) S5 n4 h* V3 x# n g" Y0 E! k
6) public string[] Split(string[] separator, int count, StringSplitOptions options). W6 R' K( v/ P7 \$ H4 m
# I M0 d7 V& V+ y9 ?: T
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* n9 z" z6 c M9 }; B# S
& O( L4 q. @7 r& b; J3 X3 o; u
1. public string[] Split(params char[] separator)( l+ O6 G2 ]. ^+ B
" O& g+ r: E% t. D- p$ w 程序代码3 v O# P, v. e' K$ O" y
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
h/ b5 n: o$ \' W) p- Gstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}% }/ v! c5 b$ ?; ^
# {0 x1 a1 G% q" y/ @2. public string[] Split(char[] separator, int count). h7 H) I! G5 k8 |" }: ~
, ^4 r8 t1 |3 s1 v: T+ ~' w 程序代码0 O# ?0 J% A3 h' A5 {8 V" f
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"} ^: r2 I' y$ P/ `! p& S" ]
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}! O; d0 N$ m9 u6 x" v
( d b2 F3 M. i! z) m3. public string[] Split(char[] separator, StringSplitOptions options)
; u2 L- D4 `+ M, B* |- c- l& o. R8 m& ?9 f
程序代码1 A! i5 z4 Z" M
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
4 ?9 K8 `) P5 U8 R5 i' k I) Rstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素9 K2 a" }9 ?. V6 A9 `1 _
2 F- Y0 b& e8 F0 {! r- C* O/ t
4. public string[] Split(string[] separator, StringSplitOptions options)2 c5 j) o6 \( [" Y& H
* T4 k$ _9 G" b, v, C
程序代码$ m9 K2 s: Z3 B1 H+ g
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素) R+ l5 `7 N7 Y# B( V* C
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5 n5 m, G' h* D( G0 m% p
% r- C6 n% y' w, z W3 ?5. public string[] Split(char[] separator, int count, StringSplitOptions options)' L, {5 R3 f' s# Q/ e% P9 s
0 i+ o5 w) _! q) k 程序代码
: o- Q/ p$ M* n9 f# `string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 N2 h" m4 z+ B- e6 Xstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素" J9 D, O4 Y. t, `% ~+ r m
: I6 h* ]( Y% S, h% Q1 l* a
6. public string[] Split(string[] separator, int count, StringSplitOptions options)5 s3 S/ r" b( r0 w5 |' H* K
% a" h, L7 t; F) h1 J
代码
L @- G0 s& V% `' Zstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素0 Z1 ?4 W0 s* k$ V# L
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |