下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
2 C# v: I. f: b' E- e" o4 L2 S8 B5 Xhttp://www.itwis.com/html/net/c/20100506/8234.html: T' v: S. D2 N* ]9 o3 U# w$ }
' ]* A# Y m% l6 b! i# d. n& Q9 ?
程序代码
! ^% ^7 b& R$ j) t; ~2 M$ U1) public string[] Split(params char[] separator)
) c4 k& l6 m, M! P' v' [% T' B2) public string[] Split(char[] separator, int count)# f* b" m: I- }9 s0 R
3) public string[] Split(char[] separator, StringSplitOptions options)( k9 \2 X6 Q6 Z' c) v8 H
4) public string[] Split(string[] separator, StringSplitOptions options)0 m8 b& @0 j& B% L
5) public string[] Split(char[] separator, int count, StringSplitOptions options)2 X* X$ S- g7 l" B# B1 B
6) public string[] Split(string[] separator, int count, StringSplitOptions options)/ O1 X3 r0 H/ n! f- d
+ O2 q/ |$ s1 B; ~: x下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
: p/ d/ U1 P R2 i, @% A6 w% B2 Q5 `% T# x
1. public string[] Split(params char[] separator)
( S# b$ ?. p( j+ ^7 m- Z3 }9 V) I; P, x3 M
程序代码
, L% _5 P% o3 l% M4 n0 jstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
+ ]! ?( o, T. W# R9 f6 vstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
, l$ I. e5 a, W1 D7 i1 _5 X: V. W* R- X m9 C7 ?$ T( R
2. public string[] Split(char[] separator, int count)
( E1 V1 d7 |- m) `# y
% @, _+ G, A. V 程序代码5 E8 X6 \. y) Q# Q* L9 @! r
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
7 L/ n9 Z/ f/ Dstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}- |' z1 W2 B* I4 v. Z9 d
% ?& u& p, Z1 W! M$ w' \
3. public string[] Split(char[] separator, StringSplitOptions options)
( m, V# w1 Z% o% r. u$ M% v7 b, G' L0 F7 k: R
程序代码
( S# Z& J) L4 D( b3 q0 Ostring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素$ u4 Y5 l6 ?' j
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: R# n& c; e; o# g- l& ]0 A) c% T$ O; M
4. public string[] Split(string[] separator, StringSplitOptions options)
3 K% C$ k1 S8 S" L3 w9 ?6 Y6 j+ f* j
程序代码
$ V" S$ @: @- L/ jstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
# d! k/ ~4 K& t/ F4 ?% |* estring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
/ U1 l/ r* b6 i1 @# i; W+ h2 N6 q- i3 ]; Y3 u$ T
5. public string[] Split(char[] separator, int count, StringSplitOptions options)+ P3 \9 S; r( s' N' B$ P
\# z* R" ?! `0 B( {
程序代码7 I7 M8 p# t" r/ ^
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
0 o# t; W: T. G$ d! Ystring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
# G8 o6 k1 W$ u% y4 Z# {1 ^3 D# J" W! u3 d0 s
6. public string[] Split(string[] separator, int count, StringSplitOptions options)! z" T {7 L; W) F, m% y# ?- L$ h
! e: D3 U( Q/ `* ~# U! k# O代码1 d+ X& G0 F e# i8 T+ {5 N
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素% T) m5 X% W Y
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |