下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看, h6 p4 W( K- H9 W) v# X" @9 N
http://www.itwis.com/html/net/c/20100506/8234.html
! |7 _2 K) h8 a" z# D3 D7 }- P6 j0 c# p( B
程序代码
" y U5 H5 o1 q1) public string[] Split(params char[] separator)
& E" Z ?6 E5 C' j6 v. r2 B5 y2) public string[] Split(char[] separator, int count)! u+ j A) v7 J: O, S
3) public string[] Split(char[] separator, StringSplitOptions options)7 E$ j9 _2 w1 p t/ `
4) public string[] Split(string[] separator, StringSplitOptions options)
0 _( n+ E8 K0 p& n5) public string[] Split(char[] separator, int count, StringSplitOptions options)
/ {* [& S, n+ M: Q) |, [. q+ q6) public string[] Split(string[] separator, int count, StringSplitOptions options)% G. G; u: G9 e( o: u
% m; L$ u7 u V$ w% Y
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
4 ]* ?& v! X9 z1 B2 v4 Q) M$ U' O1 p, r% i
1. public string[] Split(params char[] separator)
' Y& m0 S7 A* z$ H5 b: M" ]+ R/ k4 s9 L$ n
程序代码6 k3 A3 V) \1 H- ?) Q$ ]
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
& u3 ~7 Y9 q3 Sstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}( Q- m4 E) m3 o* G: l; C
' M9 z' c1 ~$ i H {+ L
2. public string[] Split(char[] separator, int count)4 f3 w. }. \1 S9 B; K$ ]1 t
4 B( e- s6 Q1 [ y; ~
程序代码. \) i) O. w/ I2 D
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
3 ~( Q1 t6 @% z0 j1 A2 bstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
6 d1 B J9 @. [1 \) T, @! m! S+ m; }4 M1 Q' t+ U- M# U
3. public string[] Split(char[] separator, StringSplitOptions options)
; r) l$ K7 N, C+ t+ f, P0 D& O. q4 `6 Y+ f! }1 d
程序代码) u$ Z) j% U' w3 I3 m
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素) p! B, i* N( a0 a; `
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- }. P7 k& |# r& Q- o! B
# ^$ ]% W A, T4 ?+ [
4. public string[] Split(string[] separator, StringSplitOptions options)
9 s( W% ^; Z9 m% i' m3 A. Y* e5 @! O2 g$ S8 f& L
程序代码
) t- w2 B+ D2 @) K+ vstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素3 x3 Z* O1 i+ C
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 m3 ?, K+ X4 E6 |, U: [" C
8 {: }) R' p$ N5 O5. public string[] Split(char[] separator, int count, StringSplitOptions options)5 T4 P6 B* b9 \) i6 m! l
+ T! T/ E; |( v/ y [5 h. J
程序代码4 }+ e, T* f! |: W8 h3 K8 j
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
* A/ x/ g% J. @ \9 T. r% {string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素9 r {7 Z; z) J( k! `: q
2 V7 I7 K: p) n" {, {6. public string[] Split(string[] separator, int count, StringSplitOptions options)2 O3 J! w1 E$ s" o1 f7 Q. I
/ ? p6 K) A! R0 M
代码
: W [% s, q" bstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素" U2 q0 }1 P0 m, D! ]: }& _9 X
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |