下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
w% {, ~+ ~6 Z9 a% ahttp://www.itwis.com/html/net/c/20100506/8234.html
, S% W2 d# q6 j4 ~; _' }. }3 \: P2 @* [+ M
程序代码* \7 o$ i8 Q- M
1) public string[] Split(params char[] separator)* k: \% o( V+ P) p; z8 v" ?5 }
2) public string[] Split(char[] separator, int count)
0 c& n1 Y0 S4 ?% Z( b3) public string[] Split(char[] separator, StringSplitOptions options)* L7 `( g! H* ^7 K& t- S4 S
4) public string[] Split(string[] separator, StringSplitOptions options)6 \6 s- n( ?( q& }- p
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
5 q) f$ @' C: c3 T8 o6) public string[] Split(string[] separator, int count, StringSplitOptions options)2 h/ N# |, `* i% k# T8 N0 r
& x, V( N: j% q" P2 t. _下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):* z0 c$ E$ u- m" F- h1 q
8 K( L R2 |6 g1 V6 H% j. }& d$ C
1. public string[] Split(params char[] separator)
' [4 D( q7 p" K, |" Q+ |" ?: a( h) e2 |/ l+ V1 _* ?: c% Q
程序代码3 C1 C4 ?8 Z* }3 A" F+ T# B/ u
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}- C7 n, u! k) d# M* T
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
- S3 W0 S3 W& r4 Y" m7 A3 k, ?9 M% J' m/ i% n3 ~
2. public string[] Split(char[] separator, int count)5 W1 G W, R7 B' J4 e6 ^
9 H, \( |3 N4 @ T 程序代码/ ], w o6 g( z2 J0 m+ J
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}) }& R; U- y z3 Q# E7 I
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
/ {0 v& v' \8 T3 b9 p
5 Y; L0 H; J4 m S8 J6 C. d3. public string[] Split(char[] separator, StringSplitOptions options)
$ z- ?$ X1 {1 C5 \4 L, i [( s) p N' F
程序代码
5 l% J) L7 q- ?( P& @string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
$ ~. ]$ W9 h" t9 }0 g2 L- kstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ K+ ^4 m7 n. f" k: M) x
: Q5 |1 Z& T! o* s4. public string[] Split(string[] separator, StringSplitOptions options)2 ~0 ~6 R: D. c6 W
7 P% F5 U- B+ F( m5 a* l! W
程序代码
1 B1 J! q5 C( t+ cstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
# x. F7 G$ |% C T9 {string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素) S/ r& N# O4 p: n9 z
- A0 g( p4 \/ a; }
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
7 t& y3 R) Y( u( p
8 ], ?1 y4 y) P" V. ~ 程序代码
# P$ k* g- g& N4 v/ V+ g6 zstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素) O8 {! i6 ^& s l v, `& }1 h" j
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素: X9 ?4 v3 U, T l+ p) e' @ Z
; H6 i% s& O0 r+ X& z8 P7 |6. public string[] Split(string[] separator, int count, StringSplitOptions options)- R: R8 X* U. b7 v7 F J+ D
' U& o" l: {! n* d! ~- u3 @代码7 R0 E' i/ w. [+ T0 L3 s
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素. ]% x! B; n3 L
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |