下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
: ^' N. _' c9 P/ [# ?3 ~http://www.itwis.com/html/net/c/20100506/8234.html- w) i8 o; b& f6 [. t! G
7 u; m3 T) o5 A- t; D# c* L
程序代码+ g! k3 t7 N7 R5 D9 N" Q$ X& B. t
1) public string[] Split(params char[] separator); A: s/ r) a* I- Z1 O
2) public string[] Split(char[] separator, int count)( O9 Z8 I4 G7 T k3 Y
3) public string[] Split(char[] separator, StringSplitOptions options)
, I- L8 _: @+ Q! r* I4) public string[] Split(string[] separator, StringSplitOptions options)# E+ x1 a% T) h) x7 \
5) public string[] Split(char[] separator, int count, StringSplitOptions options)$ G8 Q3 q) Y! N9 k) \; X" R
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
, B1 t2 I+ s Q+ b I7 ^' ?- D, M+ |& b1 X4 K- M0 d, n
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):" I% H1 A' n9 n" S9 Q \8 x
. t; m1 n9 {+ @4 M6 A, C
1. public string[] Split(params char[] separator)
- D9 _+ E3 ^# {+ A
9 i* L+ K+ Q& Z 程序代码
j' B9 L, Y& Xstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 H) G G& v6 x( G
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
! Y" s: S# L2 {% k. ]9 J7 T/ p5 S$ p
2. public string[] Split(char[] separator, int count); }- ]0 s4 q- u+ q# }6 C& Y! k
( Z$ r( B2 P0 w
程序代码
) h) `1 P6 f }) m% l+ _string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}4 r/ o0 I$ o1 g9 p
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}* v( H1 y) j$ o+ P! _" M* _
( Q! B, c) |& Y( n9 ]1 |
3. public string[] Split(char[] separator, StringSplitOptions options)
' b6 N9 z: ^; Z" M% o
- d! |* G0 [+ O8 K9 t D. b 程序代码
; V1 J! }3 z! |8 B/ Estring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素- o& a0 w6 L' |9 g* S
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
) j, C! _# X) [3 M- w; b
/ l6 w5 ]& D; ^- X, `6 r0 U" f, ]4. public string[] Split(string[] separator, StringSplitOptions options)
3 ?( a1 Q1 G$ T
% H& y3 P/ A W" w 程序代码
1 ]1 g2 c) f* Istring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素8 Y, [8 f& ]. R! z; E4 x j O6 Y
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素: Y- B, b' l& f3 ?5 S
+ g' f8 T3 L4 S" y1 V9 W
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
: [0 ? j* y) J, K R- X9 n5 ^/ l* ^6 l
程序代码) a; l: _6 U+ f2 Z/ R0 i
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
6 V- E: X4 \6 t! p: I$ J0 T# Fstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
) I+ q F8 W; o' q8 a$ L" ~# G5 q/ v% A
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
/ c) B, _; E9 ?; Z, R! T) { j6 ~" @0 D5 Y( F% ?
代码
- \8 n, W( }; b8 V* dstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素! h8 i, k! P: P+ O- [: \( Q* V2 a
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |