下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看' Z8 z# R$ H. h7 T5 a
http://www.itwis.com/html/net/c/20100506/8234.html' q8 N# e8 w# l4 Q3 i
T6 L2 l: N& z0 \" ~# g7 I
程序代码$ }7 y" j5 b5 L
1) public string[] Split(params char[] separator)6 a2 @. J3 ~/ J7 A
2) public string[] Split(char[] separator, int count)
% w I7 t& R! K3) public string[] Split(char[] separator, StringSplitOptions options)2 d, j3 N- @9 |( e$ g+ P; `' D) _
4) public string[] Split(string[] separator, StringSplitOptions options)- _. L+ T( I/ }7 {, l U; I# _7 w+ `
5) public string[] Split(char[] separator, int count, StringSplitOptions options)8 @8 a& P" H2 F# A3 D$ N" v0 s
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
: H# \9 i4 Y$ ]* w2 P8 a' x( |2 H* Y
, t& d% x. w+ M" E下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):6 Y( q+ M$ U4 ` @. ]8 v
5 f. d" t" J3 `7 e7 J+ N
1. public string[] Split(params char[] separator)
) l1 _4 C* Z* u" f9 S8 |4 B9 U. F* G/ V$ u* E
程序代码- g5 W/ x( m" u. u% @ t: {2 P
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}& h7 j. ?' _. F% f9 V" u
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}9 A0 q( l1 L5 y% q/ V
/ X/ k5 r3 ^1 o2 c7 F6 m2. public string[] Split(char[] separator, int count), U+ @( }% Y6 G/ `$ m
& u6 n9 u1 L( e& [/ A7 Z
程序代码
5 t w8 s" a; D( v: \+ ustring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}7 t+ {) O; v7 L" X- r' `* R
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}$ R n: \0 w, F6 X& n0 |9 z7 ~
8 Y( f [. b4 ?6 ~% |; J7 V
3. public string[] Split(char[] separator, StringSplitOptions options)
5 c4 _, H5 n# U0 a: J3 \7 w7 _
. d; L1 w0 @9 ]/ O. f 程序代码; l! a" {1 h3 S7 U, [6 q# F+ f& _6 Z
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素' a1 V% L/ }1 U2 [" H( s
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ b+ s5 a3 \) I7 {3 \
+ Q1 B ?, i3 m8 }( l$ @
4. public string[] Split(string[] separator, StringSplitOptions options)
5 D8 ~; Z7 h5 p* n
- i8 N0 A/ a: e% s- o( B2 N& P 程序代码
$ @ U% _3 O2 Q. w$ tstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
6 \- k k* q# r& }" zstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
# z" T) ^9 v: Z& S* R3 m
( w& r1 g- | R3 x" G; r5. public string[] Split(char[] separator, int count, StringSplitOptions options)
, U4 H# c2 j. C9 N9 N" t( g
8 C: u4 {1 e) c1 U9 X! Z, ? 程序代码' i2 M( y; O% H# j" B- `# U
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素3 S- _: b/ B; X
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素% u1 z7 R( p$ v' Q
( s7 P" S. U% S6 u/ |- w) `4 A' h6. public string[] Split(string[] separator, int count, StringSplitOptions options)
, M* }+ O. _/ }0 ~0 T, P% n- c5 b) ?2 C- L3 [ Z" I" `3 I9 f
代码: D3 F/ `! n7 W* v/ r5 O
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 P: Y: J+ d* F2 tstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |