下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
- F) f! V9 t7 X! K3 Q4 Nhttp://www.itwis.com/html/net/c/20100506/8234.html
. p$ ]# s0 L3 l+ Z, w5 Y$ J o! u Y% m4 Y
程序代码3 X7 T# n" I4 Z
1) public string[] Split(params char[] separator)! K$ C5 U5 D7 F7 P
2) public string[] Split(char[] separator, int count)( P6 v1 I! b: ? D8 B. O9 Z
3) public string[] Split(char[] separator, StringSplitOptions options)
0 R2 P7 a! y3 ]9 O6 D& o4) public string[] Split(string[] separator, StringSplitOptions options)
4 _$ i) A6 \8 S, D/ Q9 u5) public string[] Split(char[] separator, int count, StringSplitOptions options)
+ l5 S! r1 R$ s! z, w6) public string[] Split(string[] separator, int count, StringSplitOptions options)
, ?0 N* r- U6 `) `; ?* W% U& R7 B7 S( a
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):$ ?- ^! S4 F# o% Y
# \2 i. }4 G4 u. _" L2 ~8 y
1. public string[] Split(params char[] separator)
* \* ?$ `& v& D7 \% C
) ?- S1 y# B+ G! a+ k' X/ I 程序代码
) o4 a: E1 o- ^7 y: Ostring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
, B- Q, P1 M& [! t' U5 Kstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"} T% b1 @. \* y7 F: c6 y
& u# o) Q! k# w: M/ Y! E! o2. public string[] Split(char[] separator, int count)0 t1 P( A: S- h, `" h$ p
! S" M& R; I+ d4 ?
程序代码4 h: @) r5 y. P4 h @$ `4 _6 o
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
6 W5 M% m3 A: a( A, h2 Vstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
% H& F% {+ E8 j7 I) \7 C3 h7 h! a+ e/ c# z8 m1 S2 F" B% X
3. public string[] Split(char[] separator, StringSplitOptions options)
- @% `; S( G( H/ E0 D7 ?' D/ W! o/ d
$ K5 O" G1 T/ X% o4 q, C; H4 _/ j 程序代码
3 v# |/ P8 c9 ^8 N1 ystring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
" u! S b v( i2 I. pstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 r! o7 J7 h; ^; Y
8 S1 m& Y, q4 v- A' j1 X4. public string[] Split(string[] separator, StringSplitOptions options); ^$ D, c) u& y3 {. y, n' Y
# p+ Q- ^5 T p/ u 程序代码/ [, K$ _, l3 ^0 W6 Q/ P) N
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
) n9 E& Q$ X# f3 B9 Tstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素3 V* X/ h6 x4 v
+ g; N# F( \$ B* @. }4 R+ i, K) O7 {5. public string[] Split(char[] separator, int count, StringSplitOptions options)
% B' ~$ h) a8 z2 [
# E9 E, X1 |* M e7 d4 ^6 f 程序代码5 W* N0 E/ Y' d7 e9 C$ Y; A/ w: x
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素( F Q& v2 Q4 h; C$ F6 z! V9 t
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
0 z/ u. _2 m ^+ M+ n# ]6 K6 F2 T8 N
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
9 H! [7 H' ^, U# U3 R& K' M( v! e" q* x
代码7 `: g* ~3 j8 X0 Z* u! a, w
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素* w& C) H: v$ F5 C
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |