下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
6 O- B- M# I2 N! O" n$ @http://www.itwis.com/html/net/c/20100506/8234.html
3 w0 Z |! E* n' ^/ b2 r
4 L2 m. L, ~! a# B/ c+ k1 C1 z程序代码' O( B, O# i) v" | k I3 B
1) public string[] Split(params char[] separator)% y4 ^8 H: z# X/ Y' |* ?
2) public string[] Split(char[] separator, int count)
& }) Q+ c) f2 z8 a& p7 o3) public string[] Split(char[] separator, StringSplitOptions options)
' F6 g1 Z6 v( t0 L# `4) public string[] Split(string[] separator, StringSplitOptions options)
) g0 X" C. A3 O5 k5) public string[] Split(char[] separator, int count, StringSplitOptions options)! v8 V7 ]6 b$ W" s
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
1 y- ^* A' q, _$ s' Z
; M0 h- @* V) M1 \: q: d) Y下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
- V, Q/ g. ~) N4 D' p
( f% M0 m" h5 J# \% q1. public string[] Split(params char[] separator)
S6 W- `7 F" B$ [9 n( [: T) @+ \/ a6 z/ n: h6 u5 p0 O
程序代码: d: W' J8 e: U
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
& g3 ?; \9 n6 R! m$ hstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
% V! o6 J' c' w: f! z$ z
) i& K' \) y; z+ L2. public string[] Split(char[] separator, int count)0 n1 g* _% }; ~; ~/ [# E8 H
4 K. Z1 ^$ B: d* z! P
程序代码8 G( Q: V/ |2 J- | a
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
9 t$ c4 t8 U6 }3 ~string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
1 O* } ^: u9 P" q0 O: g
/ J- p+ O+ J* c9 n! B- F1 w, t3. public string[] Split(char[] separator, StringSplitOptions options)- |; y6 K* J9 L
! d1 F" H$ w" t# L8 s/ v# H
程序代码8 j/ r- m% _2 B2 C" f5 j
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
2 r' j& d1 C& N, e4 v5 w, {string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
+ e9 s$ ?; t2 G
% t3 w2 s' N6 Q& R7 j( e9 E4. public string[] Split(string[] separator, StringSplitOptions options)
+ u5 ~6 C4 g8 q, g. K' V; }. Q, F6 k6 T5 n- A) ^
程序代码
" }' x2 G) l- p4 B% Wstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素' o5 E9 R& g( Y- _
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
" v T! O m& H- R' a! x9 g, A. @3 N- d& E' m
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
+ z4 C0 ^: z5 R) e- G) W2 Z& x+ U5 E" n
程序代码
G- g7 f, S9 {4 j( b3 Astring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素/ E7 K' K3 o- J6 J
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: e r7 ]2 a6 U0 f( o* y; y. X$ S# c- C- e$ k
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
7 ~& [6 G9 }1 R6 D; q5 J y: G6 v" s; c. F0 a5 `/ |. E9 E) Z
代码
3 T( a0 p6 w: J7 \. astring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素8 _' v" f. [7 n/ d( K* m3 j a
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |