下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
: u" t* \: w4 ~8 V u) z5 P5 dhttp://www.itwis.com/html/net/c/20100506/8234.html
* L9 h$ ? x2 A) n9 v& t0 \$ V
6 c. y2 W; a7 c. s6 r3 [7 y5 w# F% W程序代码
- @$ W' x6 Z' Z- s: J- C7 p1) public string[] Split(params char[] separator)
- r( t- L9 a- `& X2) public string[] Split(char[] separator, int count)
0 v) y" h2 ^: i3) public string[] Split(char[] separator, StringSplitOptions options)5 @' z$ U3 | X$ K4 e* D
4) public string[] Split(string[] separator, StringSplitOptions options)5 ~# A* ?, l) s" G5 m% W6 r
5) public string[] Split(char[] separator, int count, StringSplitOptions options)! N# o6 A& C2 M p/ |' {* F
6) public string[] Split(string[] separator, int count, StringSplitOptions options)1 \( o2 f1 X7 B H
! {" [/ {" {0 a* i9 F- [3 A2 m下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):' V+ m" X7 v3 n0 ^+ x1 G
* {0 W! ]6 t+ o7 W+ k. B3 N1. public string[] Split(params char[] separator)$ U- ?" }4 `! ^/ g( g+ g
5 b4 M, @+ j" }; @% P3 l# S& h9 ` 程序代码: F( d/ y4 O1 B2 k
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}4 _# E3 L: o5 K8 h0 f
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}- \6 k/ R2 l F$ D' y
% r4 P& T# t2 `: U- o% e, f* R2. public string[] Split(char[] separator, int count). o+ P! G7 @: [5 K' h7 r
' V$ }! a- u/ l h1 D0 N9 a$ ? 程序代码
3 p. n5 s K( |2 wstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
" T4 ]# s5 C! ?string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
- W/ S& U0 F2 }6 } K* }, C
$ @* ~7 s$ B) b$ S+ n3 R3. public string[] Split(char[] separator, StringSplitOptions options)4 x, I; S8 o4 v, P4 {
" Y) N8 ^- E8 N t9 T# \ 程序代码
3 a/ o# R- P; D- ?7 r! }) w% |$ s' pstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
6 G. Q) `, Y ?8 `6 x2 l8 X4 v" jstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素7 \7 I9 [- Z3 N7 G
- S k0 C6 R. ^* m& e" ~* N! V
4. public string[] Split(string[] separator, StringSplitOptions options)
4 l0 H& L7 l t Z. @3 o5 N! A- r% u) L+ F! z2 W2 w
程序代码3 `. z$ Y1 P+ Q/ f
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
1 i7 o8 V9 L7 O' zstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% t: x8 e/ T( |! I5 ?4 E0 l4 F% S j& J: L# J* e% X
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
4 O. n$ O K( x' S$ x( P* I- g- S% s. A( `% H/ o4 Y
程序代码
' q* n" O. m( m9 t( I( j, rstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
5 X9 A! W0 Y$ Wstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
4 N8 R* A7 v" o9 U& M s# v$ Q# Q
6. public string[] Split(string[] separator, int count, StringSplitOptions options)9 R7 G& f) r) S! l `7 w" f7 e
" R+ U& X1 ~! E4 a+ r2 F/ T
代码4 {' o$ { M& D& T
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素# C+ c9 V0 o! o' e/ \/ T( w' O/ i( I
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |