下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
* `& ^5 ^; F0 B" ?& I6 Y: `3 X/ Xhttp://www.itwis.com/html/net/c/20100506/8234.html0 l' `9 \" U( @4 ?3 h. X
\5 _0 p# B* ]程序代码
1 l- q' A* U9 J- I X1 r4 F1) public string[] Split(params char[] separator)
$ j# |; F! A/ W5 t, l! }- P2) public string[] Split(char[] separator, int count). f; i$ |4 G+ M. [
3) public string[] Split(char[] separator, StringSplitOptions options)! @3 Z8 Q9 J# ^# c9 F$ I- z
4) public string[] Split(string[] separator, StringSplitOptions options)
+ T6 V- \6 O( ?5) public string[] Split(char[] separator, int count, StringSplitOptions options)
! e* a( o6 }) A* I# j6) public string[] Split(string[] separator, int count, StringSplitOptions options). b, B* O; P6 O7 d
2 p" i4 T, e% D下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
* x/ K% f2 I# V' I9 h
2 V; S& Q/ O& K L6 |1 \1 g- h1. public string[] Split(params char[] separator)
" v, o' A% M0 J" p# F c+ y& B- `% v3 d* H
程序代码
# u8 A- Z8 \! G: M1 Astring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}& j. ` l) X7 y( E8 t2 I" G$ c
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}" E! d9 S v- l& h U! X5 o
' {" C7 b8 F+ t" n4 Q
2. public string[] Split(char[] separator, int count)
- J. p `$ G! M* B; ~
* n* R) c. d( L5 } 程序代码& b) e2 X6 D4 x6 W3 f4 u) ?
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
& M- W' n* x, C6 g! \( Y `" vstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
& t( a/ p9 Z: p# }0 Z
* G7 ?* q. C$ C+ ?, g3. public string[] Split(char[] separator, StringSplitOptions options)
" ~5 b4 o8 i) ], n. F4 r) D, O* n- n( D. J/ C: ]6 B% {: S
程序代码
/ q I" s* x, R2 f. N3 ?# l3 u8 @7 Vstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素; U% w) B/ \8 n& e5 m
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 Q$ h% t- o' ^5 z q; y! j2 T
$ x1 d# q& @# D' H- _% I4. public string[] Split(string[] separator, StringSplitOptions options)) o2 h: u: Y! T( f; V0 d
6 b7 c4 w1 k7 Z: _5 R" O
程序代码
3 U2 K8 h& i* p) q! B7 astring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
8 S& R2 }- v; ?$ ?3 E3 Rstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素1 H, p! p# e; ?" t* [/ r
g+ L0 {8 E2 k7 p( X$ {+ ?; M
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
$ D4 f- B x g6 u! }" U4 [
: g3 U: o8 Q" T" A 程序代码
* `# X2 h% P5 c8 wstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
& C# r5 M: K; p& w7 |" l7 A7 p0 Lstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素2 J& u2 [* s+ d" {3 S- D$ _
8 p2 `# e/ W/ M
6. public string[] Split(string[] separator, int count, StringSplitOptions options)1 \ D, v2 K- \1 D! ~
! |' P6 z8 z/ k$ @/ ?" [# i
代码
6 f: F1 K+ ~1 v4 ^4 e( rstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素% O- b+ z8 i+ G3 M9 Z3 l
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |