下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看0 b" i! C7 |4 P. N4 A
http://www.itwis.com/html/net/c/20100506/8234.html
- U/ B9 \: U( p
/ d$ t9 `$ f4 v$ O1 z1 n: x0 j9 \程序代码
! A4 l# F3 ~: @9 u/ y1) public string[] Split(params char[] separator)8 {$ F* j. O- W
2) public string[] Split(char[] separator, int count)
- C, S; y- Q0 ~, i i3) public string[] Split(char[] separator, StringSplitOptions options)! p5 ?! _2 Y! r" C0 d
4) public string[] Split(string[] separator, StringSplitOptions options)
0 |" o1 v3 L/ r7 ^; K8 r5) public string[] Split(char[] separator, int count, StringSplitOptions options)
4 Z( z( Q( i6 d) k* b6) public string[] Split(string[] separator, int count, StringSplitOptions options)
( w9 R0 L. x2 ?$ z3 m- J7 V4 J0 i0 p' x& {$ M+ C; c' e7 K6 f
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
1 o' I9 o; z2 Q* b) U1 ~3 D7 W" J3 z/ r8 U1 Y4 \. a
1. public string[] Split(params char[] separator)
6 }+ ]0 ~2 D) O5 q
7 ]6 c) C4 W$ x# B: i 程序代码! H# w8 _: Z, x! ?! @6 c
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}* ^; @) D: J6 R f
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}# `; F# w- S3 Y) z; I
8 c" }& C+ H3 y7 F3 L& k
2. public string[] Split(char[] separator, int count); L3 I' V1 D- a/ O' i$ n! t
# M- i3 F' y5 d' g; A0 W
程序代码
) K0 U) J# F" o2 G+ ustring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
5 I; v A h/ B! P/ R! xstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
8 `& j7 p) p. f4 q+ N
. F) O' E. {6 b$ D3. public string[] Split(char[] separator, StringSplitOptions options)/ ? J! @! h M8 K4 N) X$ F
' r+ u0 e6 D" s 程序代码% W% Y" K }9 B. j5 ^8 `
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素; c9 \3 ^9 u) w! f& {
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素: M/ X- w5 X: A& e8 I+ k( x
2 A) v& o; D2 F& i' w1 m
4. public string[] Split(string[] separator, StringSplitOptions options)
9 l9 d6 {( t, J( G' F6 a1 b0 j1 E. x# Z. { `7 q
程序代码/ B) j5 d* [4 w
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素0 y6 E: ]2 i" ~( |0 {. y: R6 K
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 ~- W: b; D L8 c6 H7 N$ C
a& F) g. O4 l! n4 E4 d3 m5. public string[] Split(char[] separator, int count, StringSplitOptions options)4 A* p! W0 O* Q
( v; [$ o! P2 y: ^$ w3 _0 X- G# k 程序代码
, ?& M- D$ p1 _- `9 jstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素. s$ M) N7 u! I7 i3 f3 [
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素& W) v3 O! W- L$ h& E3 r
2 ]+ h8 x0 W7 I, x3 C6 {$ ]5 g" f
6. public string[] Split(string[] separator, int count, StringSplitOptions options) C4 J' z$ W4 a5 k
# p6 b& z7 M U2 U9 U
代码
) O6 f- I+ c3 w3 M( Z p4 Astring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
1 D- ]* ?/ H) U8 }) H$ pstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |