下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
; K0 J/ F& E. i& V& g- ahttp://www.itwis.com/html/net/c/20100506/8234.html$ f/ L6 K9 Y$ c4 s/ y8 a
) ^! v7 M9 @8 C, Q* S
程序代码
: {5 t( q) g2 s1) public string[] Split(params char[] separator)
7 L0 ]& y* \% X+ ~2) public string[] Split(char[] separator, int count)
% w, L. X6 C% d$ U3) public string[] Split(char[] separator, StringSplitOptions options)
, h, Q/ c* T$ g1 Q$ c4) public string[] Split(string[] separator, StringSplitOptions options)2 q# ~) R: Q2 a" Y. s
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
+ ~* v5 X6 r# j2 L; ~6) public string[] Split(string[] separator, int count, StringSplitOptions options)/ M1 X7 }% R/ a( s9 o6 l
, @1 y) u% A; h2 y0 U. Q# O
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
/ ~* } H+ W/ K' N) ?2 N, x8 Z6 R2 k5 c2 _$ m; {
1. public string[] Split(params char[] separator)
" L# T& @5 v1 g' P) k% H% x9 _- W$ O# ]: D1 s h
程序代码5 A. M! A) m: H' n. [( x( h5 ?
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
* u) d. A4 J0 P& lstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}: I( }4 x: G2 J
& R/ \2 e" g* w( v: a1 D
2. public string[] Split(char[] separator, int count)
1 @1 h0 m8 E) q/ O* ]: B4 A5 t
' W7 N8 o# N) j1 A 程序代码 g$ q. L4 B8 p5 C7 e) \
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}: M1 i4 N6 f1 f0 t
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}& J* U- q& t$ h+ J1 d) c% u2 D
& L; A' ?: Y* O% g9 U; U
3. public string[] Split(char[] separator, StringSplitOptions options)
& D8 b! R) s& ^8 s
# G1 u& A9 w4 B- j9 m: r6 E 程序代码
+ [' R& M: x, e' f( Bstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素3 k! p& Y9 B$ C9 r
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( |/ w8 A' r+ w) p+ h ]
* q. ~. m0 b7 q, Y3 m4. public string[] Split(string[] separator, StringSplitOptions options)$ Z" x1 u0 [: n; Y
, i" U2 L* ]0 S* k 程序代码
' a8 O3 {$ t$ a( f _# estring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素$ U+ S* X1 O1 g- G3 x
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, u; t% A' Y/ Y3 Y- `2 L% F
/ ]5 [9 u' o0 h/ |$ ~
5. public string[] Split(char[] separator, int count, StringSplitOptions options)& g4 V0 ^' \9 L6 ]! S$ G/ z
8 J: W- c. g& N9 M3 O 程序代码4 o5 {- L3 O8 o+ m1 P
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
1 l2 ^& A1 O: dstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素9 c6 c- `1 }2 ^( j$ u. ~6 T( }
" g! C, s7 h3 M, j5 v
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
q t6 d& l! E
0 ^# d! {5 w5 a0 b/ T代码/ t$ w, J5 d) j# N, ]
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# z+ q% [7 u$ h9 I2 F) p: j: H+ C& Dstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |