下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
% b" I4 J4 E1 @ `- x. X' Z7 H7 X# yhttp://www.itwis.com/html/net/c/20100506/8234.html3 F- q3 |3 F4 F: g
( G- u& ?8 V* o- b+ f程序代码$ V) E, _3 X+ U
1) public string[] Split(params char[] separator)
2 D6 q8 X5 S$ b* f' ~- }' `2) public string[] Split(char[] separator, int count)* G: A. e& L( `" J, h: i
3) public string[] Split(char[] separator, StringSplitOptions options)
: B- u6 ?4 ?3 A4 v8 Z- `( h% G& l4) public string[] Split(string[] separator, StringSplitOptions options)
; {0 y! x' W3 {- S5) public string[] Split(char[] separator, int count, StringSplitOptions options)+ \7 k# Z8 f3 ~, U' u% x
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
2 o$ @, M0 Z/ l: }8 k3 g3 _# Z3 q! Z2 ~- @
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
, E$ @/ I' C( w- ` l
: b; o3 t7 o Y! H9 H" ?1. public string[] Split(params char[] separator)
: c# z! C' U5 c4 }0 }: r6 @; x9 J* b# }
程序代码( c6 h& R8 ~" m: Y+ u0 S
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}) A. ?% B% K# c" q) F
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
% U {5 h, \# r% M( o9 f+ a+ h2 V
2. public string[] Split(char[] separator, int count)
$ X1 |6 Z7 s1 I& F2 c# |! @9 T6 a- b
) @* ]- |- J1 N+ `6 ]+ h 程序代码& i& p& Z! p6 j& R
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
3 j0 d+ Y7 u# x! U/ kstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
2 O" |* E. z% k
n) [9 Z8 U3 L$ w3. public string[] Split(char[] separator, StringSplitOptions options)5 H z! b" }8 Y4 E
! t- d. U$ h" ^0 I. c9 c7 c9 C
程序代码/ }4 _1 u$ K2 o0 g
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素7 w+ E4 F' Y1 ^& }
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
h# A1 }' j# s1 B- D" `( D
. n% C% H1 A3 ^7 s" F2 V5 E4. public string[] Split(string[] separator, StringSplitOptions options)8 v# _6 s3 x3 f" O7 p2 [
& g# A. o6 Q9 u* W% r8 O* }* F) A
程序代码; M% x$ X7 E- W: \
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
! K# b0 _, ?& q0 u1 E8 a/ _! ystring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
- m7 {+ \; F; e' j
3 l' e' T8 U, W" Q5. public string[] Split(char[] separator, int count, StringSplitOptions options)+ @! I# W2 ?3 X7 A; ~, i( X
" H* ], h& u; u4 U, \$ S, K, N
程序代码
7 @% I0 o+ Y7 } b8 |: Lstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
0 t. j& z+ ?. b! wstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
& i1 [/ L5 d# V+ u# @# a6 s
+ ~" p, \# a7 I6. public string[] Split(string[] separator, int count, StringSplitOptions options)0 l; R3 ^) Z+ u" R$ w
7 |5 K( L7 f+ h: n* c. J1 Q) g' N代码/ K- `; Y" x5 P0 a2 P( E% r+ P
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素% R4 B8 A. @- y$ f
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |