下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看& Z7 {% o3 t5 k+ Z% v
http://www.itwis.com/html/net/c/20100506/8234.html b8 }/ o! o" p- h+ G
: M/ S, b `# m* q
程序代码9 M5 `4 b9 F* f C7 \. N+ r& N+ x {
1) public string[] Split(params char[] separator)
- l# d8 q6 i3 b2) public string[] Split(char[] separator, int count)# U" H" ]( V' G: M
3) public string[] Split(char[] separator, StringSplitOptions options)
7 F, G3 \0 O* N9 I! M! `% P! D4) public string[] Split(string[] separator, StringSplitOptions options)
9 {; g% E( G' D5) public string[] Split(char[] separator, int count, StringSplitOptions options)
, I. f# `7 |1 R( Q; j% |6) public string[] Split(string[] separator, int count, StringSplitOptions options)
/ s, O/ [7 u0 K `. s& R! D% |9 d" O& }, y4 T
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
3 u. ~7 S. O% C! E N# z7 j9 r
" g( V8 x$ Y* M5 w4 T+ c1. public string[] Split(params char[] separator) P+ W9 s5 d9 j2 @6 a
% Y; Y9 K& D9 H. h" \
程序代码
9 M' F, W; U9 H, u i% [6 B+ e. mstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 Z. P0 a W4 g, @4 P6 S
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
, _, R, d+ W0 H
! p }! T% K x. B) Q; K1 H: d( A2. public string[] Split(char[] separator, int count)6 s* r3 L, o& B* O' [) y" ]3 T
' u% P4 c6 ?( A# L0 X/ w( ^8 }7 R 程序代码
* z2 G3 ~6 i; U5 W/ R9 [ X1 bstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}+ S8 f1 Z" b. s# |
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
9 ]# ^* B; p, G9 D% C
# p: ~5 t m% n) K3. public string[] Split(char[] separator, StringSplitOptions options)& e- C% b' ^4 H+ [
9 b2 X6 x! A/ }# q 程序代码
2 h' a9 p' k* C5 P' Q$ Nstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
V1 }- A# J7 g' X3 o: d; ~string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 M2 F2 W* @5 N1 @0 ~8 p5 V
8 j% L/ t Z% s) s3 w
4. public string[] Split(string[] separator, StringSplitOptions options): f5 f9 A5 n2 C: e3 g
+ \& b1 e* T( w7 V2 H- P/ F8 u
程序代码
( f+ k+ {6 l" ~+ f' kstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素( a$ X: Y, Z& i! N
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
+ m/ \; P9 K6 ^, \9 Y- y" F
9 w. D- [& U( y4 q5. public string[] Split(char[] separator, int count, StringSplitOptions options)' e7 x* N ^" A$ X$ L$ g
% \ b+ z2 j: N d3 @2 K" U4 x
程序代码# O) G* q5 s4 W- s2 I3 [" v
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素( w0 z8 F( g: _# a) F; U3 e
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素6 }/ A6 h h4 D- b
) s5 l4 J& c/ e5 C5 ~6. public string[] Split(string[] separator, int count, StringSplitOptions options)
+ ~* [7 x, d: h9 {3 J+ H1 c6 \( b9 W! X. x7 f
代码. P4 e8 u7 J8 X* c/ }6 g
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
( T4 w, V! T4 s$ A# A& B _string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |