下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看( R, D, H( W3 x g8 i2 J) G
http://www.itwis.com/html/net/c/20100506/8234.html, l% J7 |7 v- l6 ]3 C
( M7 |8 E! t. R3 O( k
程序代码/ w! F* n' K' }: p; ^. l3 }
1) public string[] Split(params char[] separator)! e# D% n q$ C( z7 W9 B6 ]7 j
2) public string[] Split(char[] separator, int count)
8 F \; P: }& `, l$ q& m: y- f3) public string[] Split(char[] separator, StringSplitOptions options)
. J* [% k/ T0 w, {6 Q2 \4) public string[] Split(string[] separator, StringSplitOptions options)
; v; \0 t- W) ~1 E- V0 q- v" [5) public string[] Split(char[] separator, int count, StringSplitOptions options)& m% a% w4 ^3 J! I6 S9 z( K/ n
6) public string[] Split(string[] separator, int count, StringSplitOptions options), _ A8 t( n" J( ^3 u
( c# x/ V! \* C8 U( X L
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
4 \5 z' R+ i- G, @5 l/ p. C; p3 ^# p4 w4 O$ A. N$ [
1. public string[] Split(params char[] separator)
; c' f7 U; K2 {: s) }5 M; m
2 A3 R! A w" ]! _ 程序代码
) O6 j. I# Q0 O; D+ Kstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
7 N) I, S' _5 W3 \string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
, P2 m( v, H$ d1 Q7 V! t: A# U! L- U+ ^2 n
2. public string[] Split(char[] separator, int count)3 O% j' j$ E' V* U
% q- F+ B% O6 ]7 M$ S; J& q
程序代码
7 b$ |: C* Z, Sstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
f, [! [- Q! k1 D* A" H" }string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
0 P/ i# p) K/ t7 V! P3 c+ T' w6 `( h6 `' L0 A7 a$ s
3. public string[] Split(char[] separator, StringSplitOptions options)
! z4 l' @1 _% ]; a7 o/ b9 P6 j& b8 J
程序代码
" \1 e+ [4 B' u, F, Z$ T: nstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
0 _9 @$ |/ r7 R0 i# Qstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% \& b; R7 X; M {0 u% L& s( P8 E% T6 R- T/ h8 ]- s
4. public string[] Split(string[] separator, StringSplitOptions options) a* i$ w7 ?; E: b
" Z6 f: }0 @" N# `/ g* Z 程序代码 \: G* X/ b$ _8 W/ \
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
" e, ?* n7 a6 @- ystring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: A; A m$ `4 {7 X* G; i
! x9 [9 h- E& A3 p. P' Q5. public string[] Split(char[] separator, int count, StringSplitOptions options)! ]( [5 m# ]0 I7 j: {. g6 \1 g/ }
N+ S( t# z# u F- s! _" s 程序代码
7 V# p& n$ a Q& }4 P& wstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
$ Q4 e* G3 z+ ^( x) Vstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素+ u4 g8 Z* \8 Y- F ?, C' E" ]
, ~" {& _3 E. Y
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
3 r6 N4 C5 N1 M7 f" v l* T6 x% l- {
# Y; U4 Q2 a9 l5 ^代码
8 C/ v* R5 B! ?1 o6 @/ I2 j+ jstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
8 M+ y; y l5 ^7 Z7 G | Xstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |