下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看# e* \9 X8 A4 o( t3 a1 x+ `2 D
http://www.itwis.com/html/net/c/20100506/8234.html
6 {/ O6 A+ Z; b; ^# I# S2 q/ L) H7 E5 j
程序代码' I+ |6 E4 Q# i G+ E/ Q, |. @ c
1) public string[] Split(params char[] separator)
2 F3 M8 o3 L, o3 B2) public string[] Split(char[] separator, int count)* B7 B- d) l' j$ ~. A5 T
3) public string[] Split(char[] separator, StringSplitOptions options)
: X: a7 x6 V/ t; S l l4) public string[] Split(string[] separator, StringSplitOptions options)
: B- t( j( B, C1 v9 D" |5) public string[] Split(char[] separator, int count, StringSplitOptions options)
: f) g- I/ f/ j4 n2 u( Q6) public string[] Split(string[] separator, int count, StringSplitOptions options)& d0 e7 j7 ]2 ^! f2 k$ o3 [
9 g- u5 ?# G* J# j) ]& n, B
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
7 J6 v+ T. J# a2 w$ H# S- A9 X3 X. Y, ^6 g+ n" `
1. public string[] Split(params char[] separator)# d' |+ y$ N. t2 ?! B# e4 ]
7 }- ?6 S3 G# h) \% x
程序代码- L5 q5 E0 b/ Q* S
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
. P3 S2 F* U! @1 X. y. Astring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}1 C$ D& W5 r N# p
/ G1 a6 `9 c C9 I% o4 d2 F) e2. public string[] Split(char[] separator, int count)
1 o! V( i* L/ v* K/ v2 l6 O* m$ d- Q: f5 J. V, x4 u) J$ b
程序代码% d- i9 [0 o# f& N x0 f# S
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}8 z5 L; y" A y' o
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
5 O2 u9 I7 h% t$ J
+ r% m2 O6 H, Z+ Q3. public string[] Split(char[] separator, StringSplitOptions options), Q/ Y, z" k# Z! G0 U0 k- h5 ?0 u
4 j5 _- ]9 g/ v! i' S! W
程序代码3 Z1 X, z+ k* H# d
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素( }( h2 z/ U6 [. M4 s- X/ E" H
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 g2 Y2 R) @/ u# f
4 u4 U; ^- T* a8 g4. public string[] Split(string[] separator, StringSplitOptions options); `. D$ ?# O" @( K4 b$ W' r
% ~, A. w0 K! S( c2 e( R: v# u& X
程序代码# Q" N; f% b/ ^* V) w# {/ n
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
' Q q4 O) Y: ~- xstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
- K) S1 v- A$ H4 h9 y
$ `8 N: s! E! b; }$ v; U( R# O5. public string[] Split(char[] separator, int count, StringSplitOptions options)" [+ Z; N D- b
3 X& K6 X! u7 U' O3 Q 程序代码1 I: h# k- B, D" C7 X5 e, V
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素' N' A; Q2 K, Q: H4 D: x& t# e# M# C
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* _& W1 R7 z% I: L7 [; m
: n; t' T+ T3 [$ G; D. ^: _6. public string[] Split(string[] separator, int count, StringSplitOptions options)- f) C0 f. K4 Y" M% [+ r5 r
4 ~4 l( d. ?! G0 B2 a4 @0 q8 e& ^
代码
& \9 a3 t/ @$ n2 wstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素' p$ ~ f& j% N3 r( P. t# j3 ?& [& e
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |