下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
) c$ B! z5 L* u- }8 F* x4 rhttp://www.itwis.com/html/net/c/20100506/8234.html/ i. a+ w4 H- n1 S( p7 w- B. {
) n# t- T2 i: C H- h+ }
程序代码* X7 g3 d+ ] D- F) K% S
1) public string[] Split(params char[] separator)2 z+ m: o; R% h/ E, h- T# E8 |
2) public string[] Split(char[] separator, int count)% d0 N9 Q# \4 U1 Y' r3 o6 e
3) public string[] Split(char[] separator, StringSplitOptions options): h6 `" A( h3 G; @- C! j: t- |
4) public string[] Split(string[] separator, StringSplitOptions options)# }3 N" {3 f: b. }
5) public string[] Split(char[] separator, int count, StringSplitOptions options)" h) w, \3 |' ^5 w M
6) public string[] Split(string[] separator, int count, StringSplitOptions options)# ~# a a8 k) k" T8 L, I
1 s i; E. F: g7 _2 K; E下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):3 ?( S& z$ `6 j( {
/ F! h. u4 w5 `5 n
1. public string[] Split(params char[] separator)1 j$ G. _5 `' e) f
4 N" s$ {5 \# L s' M 程序代码
& u* F$ e5 c2 Q$ c! }string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}* M! y5 X7 F* }5 Y/ j6 D6 c* w
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}, S9 t- A5 E" L/ ~
' F: Q4 l' ~$ R
2. public string[] Split(char[] separator, int count)
7 m4 A/ O6 c) z* E. A7 L+ J, R, o2 M* e; j: T% o' @4 X# T
程序代码2 O) F( S2 a4 w* M
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
9 H2 O% D9 J2 U! }string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}4 D0 ^& C# Y. x* G/ D# P: F
2 p* K6 B0 L0 q" F! V& D! Y- L1 c
3. public string[] Split(char[] separator, StringSplitOptions options)# t! l9 {+ {/ B7 k0 k$ L! d
( z; }! N' `, d; }+ k 程序代码
- ^: \! i' t# Mstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# L, }, S% Z+ Y9 L7 [9 c
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
1 a3 j, g2 a) v: O% E f7 L' z; X- }( s- W: Q I6 e
4. public string[] Split(string[] separator, StringSplitOptions options)) c' L3 d; k' x# o- G
. t) i& n# q3 A0 I4 ~* R% C0 O
程序代码4 O" f2 K+ k6 k7 @. t ]7 ?
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
+ C& k2 X( w9 A4 C( qstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
8 o1 c/ }; b6 u0 v/ s7 }0 I# K
/ i/ O6 ^" G; _ l8 m5. public string[] Split(char[] separator, int count, StringSplitOptions options)
7 d# W( k. o( r$ Y
2 ~6 q! R$ y, J' }4 l+ g 程序代码7 m! d- W5 {- q3 _
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素, T9 u5 O, U0 u* i
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: M5 Z; C1 i. z$ B) H* W
* Y0 f. ^# ]! i- [1 u9 y6 m6. public string[] Split(string[] separator, int count, StringSplitOptions options)% z3 u1 g S7 \, z0 C; b+ P
+ Z5 R7 ? _3 d9 w# `/ K代码. e& I1 p: k2 O
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
6 F% [1 `4 a N3 u, S* ystring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |