下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看6 A) C& r; C. |" |- @$ P
http://www.itwis.com/html/net/c/20100506/8234.html
8 J1 ^% i0 f, ~$ w% g9 L' ^
a( @* b7 p! ^1 P' ~& t+ h& w程序代码
6 `+ R: r8 t1 H$ c$ y) t9 o1) public string[] Split(params char[] separator)
/ x( _: N! f5 W2 G2) public string[] Split(char[] separator, int count) w3 _$ T; D1 N% ^( D
3) public string[] Split(char[] separator, StringSplitOptions options)$ s8 d& y1 T$ G5 N- e5 }) e& ~; R8 n
4) public string[] Split(string[] separator, StringSplitOptions options)6 Q5 S9 I# G( B& ]
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
% d1 ~% ?) p* U1 f. G0 m9 q0 ^6) public string[] Split(string[] separator, int count, StringSplitOptions options) [- R/ F+ ~; G' P+ A5 S9 c
" _" e& b# K! Y3 D! b) ~$ I
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):( d6 B. y' D. c! m* ~9 F
0 p% y* _ e+ {% ^& \
1. public string[] Split(params char[] separator)
. Q6 \; K1 _4 e! n$ C- p0 ~# j; [" J5 P" O% M
程序代码
% ]9 a f2 p6 ?4 t* fstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}1 z; O3 P& W2 H
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
- ~5 ?0 P7 b' ]- @. m* O* B# T) k( o8 W( {
2. public string[] Split(char[] separator, int count)
# `: K2 f% { E h$ B) i) K4 l3 r: t) H0 {. i, i/ z$ c
程序代码
2 b! t6 _) ^7 K0 J O: ^% cstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
2 }/ z @' h' Dstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}0 i+ N8 s$ M' t. I* h; {
0 H) y4 n" S6 J6 c* U( n3. public string[] Split(char[] separator, StringSplitOptions options)
8 M A6 i( [. \! J! w3 [/ p& T& o2 e, v" W
程序代码
& O2 \7 f5 N9 @string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
( J- T/ P6 c" Z' I) i, k4 d) istring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
9 r- O {! d: M6 L9 m7 a
; I9 R# ^ K& h. N4. public string[] Split(string[] separator, StringSplitOptions options)- H7 h m' s4 o# K+ H2 F
# z* g" N q8 I; Q 程序代码. |% U1 `7 Q; C
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素# j# f( j$ d6 s. H& D) a% b
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! W$ z n8 s" y% h* B( X
2 o/ y) f- j6 a/ j
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
! h5 ] z, D# }- L4 N
( u! G( h3 }6 c- y, K1 D; ?; O4 b 程序代码6 u/ w8 D' T$ E1 w% t$ x
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
' ~4 `1 a- n" Q. I7 s9 X7 Tstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素0 p& c, j, ?2 i; \4 {* |1 j
7 n) P, ~" v1 N9 _6. public string[] Split(string[] separator, int count, StringSplitOptions options)* Z! g; `3 j$ O" M* P1 M
' R8 n& z& p8 g4 O- Z: {" l! L代码* @! b9 I& e# r V& i3 [4 r
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
0 F& C, ?3 I7 ?; h( o& vstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |