下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
1 ^2 L; X* a) y) b [http://www.itwis.com/html/net/c/20100506/8234.html% @. O8 }$ z0 j5 r5 @, ~
7 r8 w, ^7 _ g
程序代码' c+ b2 N F( N) Z: _) d
1) public string[] Split(params char[] separator)+ I2 ~! ~2 ?$ L
2) public string[] Split(char[] separator, int count)
2 ~$ Y$ d9 ], Z+ \3) public string[] Split(char[] separator, StringSplitOptions options)" q3 y$ F5 C. t0 l- m: U- R4 T
4) public string[] Split(string[] separator, StringSplitOptions options)
. X* n! b" \( c0 G9 v0 S5) public string[] Split(char[] separator, int count, StringSplitOptions options)( H9 n# b& ?7 j# j1 q" J0 c' w- I
6) public string[] Split(string[] separator, int count, StringSplitOptions options)3 d6 ^& k( @# L& ?
0 |# ^3 S/ n4 C' S' K: ~下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
4 A: [& Y8 N! e: f. _6 H+ ]/ n+ i5 Q# m
1. public string[] Split(params char[] separator)2 W3 z2 ?0 D# y# x6 L
2 p8 \3 }; b* p* Z( ?% ]
程序代码. W8 G: |7 t# D% V. b9 B
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
; B- G- v( k2 V% a7 D6 `; estring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}- o4 ?, c9 e& k. W. @) n& Q6 g) e# p5 c2 Y
7 j4 z9 r0 r* F) K! I2. public string[] Split(char[] separator, int count)
9 J$ |/ u. h7 f& x n6 u3 E' W- J7 i P4 B* H2 q) l
程序代码
9 c. n8 G% y2 }: X' Y/ k! @+ ^string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
2 f. {0 S! G4 v$ E/ \7 vstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}0 z7 D; L) }# Q+ ?, d# ]* t
! P1 K2 j4 F$ [$ p
3. public string[] Split(char[] separator, StringSplitOptions options)/ S& N5 k% u% h" e/ m
. o i7 G+ w1 @$ k# Z& g1 L 程序代码
* N$ m# n2 ~. {+ W8 G' s0 T, kstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
7 e7 o/ c P/ r' Lstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
$ f" s5 D5 x( I( ^
E/ A! j- J" }; `( y4. public string[] Split(string[] separator, StringSplitOptions options)( Q$ p9 `3 R7 F. _
$ Y8 K6 X. M* P; w# C2 i
程序代码8 m* i! Q- O8 m3 |/ Y
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
" F, L% V( g0 ]* J9 ostring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素' Y* q8 z) I" L
9 b$ a1 A2 b5 v' r' ^
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
" d* a6 k( @/ Q. o1 h1 |* o: Q
* `0 `: {- f8 D n" t' ~/ X; |2 k b( z 程序代码
h9 b3 h; D4 [* ~string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素- X4 m( K+ d" J1 Q3 U
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5 ?; H1 D+ l& X2 e
* C; `) M e1 C; {2 N5 R# h& l6. public string[] Split(string[] separator, int count, StringSplitOptions options)! o* W0 E! R) d9 v9 J ^+ L
* Y* I( c" ^9 U# I0 O# v; Y& {
代码
. H( z/ R$ _4 M8 P+ u! \0 P1 Estring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
+ N4 \' @" G# |- ^string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |