下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
" _: Z! }' ?# Y: ~4 Fhttp://www.itwis.com/html/net/c/20100506/8234.html
7 [- A( |' O) ~4 B& s% P+ Z& p D# c* t
程序代码
7 O2 r( o& E7 k; H. [9 K4 n$ G5 C1) public string[] Split(params char[] separator) X' | E% v3 Z& O" v- |
2) public string[] Split(char[] separator, int count)
8 O' Q# F% I; s# [3) public string[] Split(char[] separator, StringSplitOptions options)# C5 F7 D3 @# X% F- J9 f
4) public string[] Split(string[] separator, StringSplitOptions options)
8 i: Y% ?* ]6 X1 ~- O3 s5) public string[] Split(char[] separator, int count, StringSplitOptions options)
. b8 i: H: t, l! c3 `9 E6) public string[] Split(string[] separator, int count, StringSplitOptions options)0 P) \2 \- }+ Q
% f0 h2 S4 x! u/ a- P+ v3 k( o下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
9 F$ M# S1 B$ F- `" X
& Y/ t& \5 Z3 W7 F: j1. public string[] Split(params char[] separator)
" V- @2 J4 x' N8 h/ L5 i0 I& |1 G% q! \/ R3 e/ b
程序代码
: s( \9 k$ O- s3 ?string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}7 u8 w0 e( T4 g: o
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}1 t+ P, Y; X2 [3 @+ R* R% A
0 h' o$ Z2 y+ H, y; L
2. public string[] Split(char[] separator, int count). k0 x# A! j4 q3 `
/ [5 i) I& |, O- B* M8 C R 程序代码6 O6 e3 f" l% _* }1 w6 L
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
- i, u+ C" M' G& Ustring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}( g5 a5 n, e7 a: e7 Y
' z% ]3 }' e% a! v- U0 i3. public string[] Split(char[] separator, StringSplitOptions options)
5 v6 R9 s# J7 m' V& T7 h+ I5 ?/ C8 I, s6 A7 W
程序代码) f: r, i5 Z' c2 \* Y
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
4 }0 c8 p5 \' Y, i* x" ~4 V8 estring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% i; ] I+ v9 S: H
2 u1 s7 F8 i, @; G$ m9 J. E4. public string[] Split(string[] separator, StringSplitOptions options)
( h; S. E/ P. B9 c& |" f2 Y# @7 y! ~( h" H7 S# {3 x: q
程序代码: L. b1 j* ]* a: z2 m5 O+ b
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
% ?: v6 m- i, sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素( E! T! v$ m9 [6 A$ r, k
u$ r3 i, W& ~8 ]. s( m5. public string[] Split(char[] separator, int count, StringSplitOptions options)
9 d8 W j3 K" {' d3 v( J
: h8 J7 l3 k7 ? Q; ~ 程序代码" Z3 O+ ]% i5 |( k+ [9 Y. C
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
5 }, O \8 B/ U; E/ x3 P( t3 H) X3 Astring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素* W6 {5 N7 D, K- U& I$ W
5 Z/ Y8 v9 o& n$ y% k
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
3 Y+ u2 F! f9 b: b" g7 [& C/ Q, S1 q ]( U' M
代码0 ]: P& m2 y, ]: m2 Z
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素! }. z; I X$ e" v& Q
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |