下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看* x1 w/ R4 P2 q4 Y, J# x
http://www.itwis.com/html/net/c/20100506/8234.html
: u; p9 n' {- {0 r! _" o1 B8 r
程序代码
3 K3 e- L6 u9 p/ n! x1) public string[] Split(params char[] separator) R ^% A- O; q/ d; \4 ~# V4 A
2) public string[] Split(char[] separator, int count)
7 G m- r' z \ R L3) public string[] Split(char[] separator, StringSplitOptions options)6 `2 Q5 ~- Y9 e* Y$ b* v/ [: X. _
4) public string[] Split(string[] separator, StringSplitOptions options)
2 @7 `) ~9 L3 y( n8 a3 z5) public string[] Split(char[] separator, int count, StringSplitOptions options)
* I! _! C3 Y" G6 _; w; c6 E6) public string[] Split(string[] separator, int count, StringSplitOptions options)
% M* J! Q6 y) I2 B2 j7 f O
( K0 Z1 p: C# V5 c) `2 t下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
- R! {: v* h8 Y8 R9 E0 T' i5 V
0 t7 } }( T+ k& K! H1. public string[] Split(params char[] separator)
% v2 m( I. i! E- r* C9 {' p+ |" Y4 M3 S4 B
程序代码
! N% r9 p7 ~4 X8 r3 ~! Qstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
4 h8 V9 l w4 f8 ?6 o' d8 Istring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
+ X/ J1 s8 ~. m/ b1 i; |3 t1 O
: ^$ x. Q: T! u) U0 a2. public string[] Split(char[] separator, int count)
% Q/ O5 @+ e X9 b/ _; Z8 m6 s
% Y- |2 g) f. K 程序代码
7 c' h: P0 ?. q# B! gstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}5 B' k2 U- I/ @' o* b
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}/ u! q. h$ C/ Y
7 U* c6 R. a7 Z4 m+ b" u& m
3. public string[] Split(char[] separator, StringSplitOptions options)
& Z" E- f5 U$ c% t. f+ W" j
/ K6 l4 U1 [5 I* V 程序代码, I4 k! T3 d& W( ]& c, \9 r; C9 W
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
2 |: s" n! `: L9 t7 U2 Q9 Astring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
" p+ R m5 ^8 v! A
" X/ V4 P0 ~. H4. public string[] Split(string[] separator, StringSplitOptions options)
6 D* p# L% L T$ j; ^, n" \7 ]1 h
/ f7 N: x/ Z8 H& d" ^0 H6 z3 v2 D 程序代码" Y+ q6 n# X. W1 D! D6 Q
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素2 u8 f, w) ?1 I7 O9 {
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
/ _2 x Q& b6 s$ ^. I& [$ K# ~: |. V3 F
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
' d ?- X& K0 {9 _; @
4 g! U/ S: y$ E( O/ ? 程序代码7 U4 { M6 f2 C# J0 E
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# i( Y+ y- j1 J- k, K3 m7 gstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6 k: F* |5 m( o! y
" @1 h( K T2 D: v" Y) L6. public string[] Split(string[] separator, int count, StringSplitOptions options)0 _ ?7 S# ~# Y4 t) y' N
1 v% k5 H4 b- a$ E4 m# `/ ?代码7 L- g; g( r. C: @
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# \) Q4 T* O" {$ Zstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |