下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
7 f- I# K5 q' e+ I+ `http://www.itwis.com/html/net/c/20100506/8234.html
( v0 o" g& M9 a7 y
4 w9 y8 G5 L0 c1 G; A程序代码
# `3 M0 ~+ U4 j; A, T$ ~4 g5 b1 D7 z1) public string[] Split(params char[] separator)
0 A5 d+ o* T, a2) public string[] Split(char[] separator, int count)$ P! v, E$ ` e# F( M9 O, g/ |
3) public string[] Split(char[] separator, StringSplitOptions options)
C. v; }- d3 d7 `4 f6 n4) public string[] Split(string[] separator, StringSplitOptions options)
! O4 T" B2 h/ s+ i9 H( h/ O3 A5) public string[] Split(char[] separator, int count, StringSplitOptions options)
& Z0 `4 W# b4 Y; J6) public string[] Split(string[] separator, int count, StringSplitOptions options)3 r0 V& ?. {# \: e
0 }0 f$ I4 ^" v/ ~% B5 }
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):% B1 U( z/ B) z: P& @/ B0 c
: }9 ]" F) T; N8 e+ F* P1. public string[] Split(params char[] separator)
% \+ O S, ?( {8 W
* s% S4 P* ?2 B% g6 s 程序代码/ |: w7 {! t9 w; F
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
* }) ^7 q& g( D% M4 h' y! S8 jstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}4 p- u2 S% Y% V
( Y l& l: C- t4 {- F v2. public string[] Split(char[] separator, int count)
- l% A0 v/ P9 d4 v7 e! j$ P
/ U$ p3 y/ H, o7 d5 M, o+ R" _ 程序代码. |' e/ ]9 F. `" g
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
2 y1 \/ P" p( ?0 @6 fstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}6 ]- w: G* U2 ]; T$ L+ P- C0 h
' `( o; ~; b# {% m
3. public string[] Split(char[] separator, StringSplitOptions options)
( y' y+ M% {: U& ?2 M- @
+ F: R) e6 c( T 程序代码
# j+ O ^1 O$ C+ v+ F. `! [" bstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素- M0 O& r& c' R* i8 J' z
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
& ?, V! H$ ]* j- _8 J% x" i6 K- L* L# R8 [; }' C
4. public string[] Split(string[] separator, StringSplitOptions options)1 @- j; C3 T- q2 w j7 H% V, I
! `5 W% k" }' ]2 ~$ i) ~
程序代码; }. q3 \# T; y; E4 q
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
, d0 _5 |1 R0 O6 estring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
: ~8 Y* D, i8 J% t2 d7 Z" c: S1 J" m# c0 a
5. public string[] Split(char[] separator, int count, StringSplitOptions options)/ D+ _8 {6 ~1 G6 H0 ~- i( A
( _' z. z7 ^$ K 程序代码* ?3 z# n' R- c5 M5 A
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
7 G* v' O+ ^& Dstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
1 x2 E: ]1 O9 J+ J& i+ u/ U& ?. i$ ]6 \5 r4 v, L* v& v m7 p9 {
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
5 {/ ~( ~2 o" r" w) Y0 p9 C' L+ p8 u1 w7 P& J) C8 v# f
代码
K! I# O2 `9 w. Vstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
2 w3 ~1 l. O( Y- p, hstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |