下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看4 m+ V, O, J f1 Y2 y7 z% @
http://www.itwis.com/html/net/c/20100506/8234.html8 @5 h- c( v, p! ~! f- k
- i! p7 J$ U' @3 T4 X& O% t程序代码
( C$ N7 y+ w$ X1 B1) public string[] Split(params char[] separator)* U' l# b6 \ e
2) public string[] Split(char[] separator, int count)* g4 [- l1 H: i0 t$ |. l
3) public string[] Split(char[] separator, StringSplitOptions options)
7 Q, d& Z9 Z4 y; n+ }# O4) public string[] Split(string[] separator, StringSplitOptions options)/ O( W l' ?! Z
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
# o+ p! d. [+ F) `! f6) public string[] Split(string[] separator, int count, StringSplitOptions options)3 Z6 ~: P) Q( T
; q, b6 c I! J4 h8 }下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
5 A+ e/ g# c i4 f! X5 |6 ~5 `7 u M
1. public string[] Split(params char[] separator)3 h, L& w% W1 P
7 P4 b' f N g; `2 u
程序代码4 T" j& J/ m; X& g1 r3 Z! o
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
" P @7 | {; {' S/ ~& {string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}) F/ D* m- T: h
: B: V2 u! p% F' i2. public string[] Split(char[] separator, int count)) f$ a8 r+ M4 Z1 `$ ~5 G& M
2 T6 D4 j8 }$ s! q( R1 S6 g3 u
程序代码
$ i1 b. ?. ^- J+ R3 t* E/ `string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}6 J' Q; S& l! e9 w' \4 e+ _$ ~7 i
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
& d% p- Q( K3 n G, ?$ T: @6 Y. H! A1 Y1 @) g. V% {! I
3. public string[] Split(char[] separator, StringSplitOptions options)" P* f; \) c5 T/ v
$ Z3 Q; t0 M! w2 Y 程序代码
" x" o& }: p5 l1 ^9 X: ]' tstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
7 s: ?/ d& P8 R' Gstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
; U# a# `9 B: y1 h& ?# J9 U# D. S' k4 {0 M5 L; g
4. public string[] Split(string[] separator, StringSplitOptions options)
( z ~% j4 S$ R, S o6 i# D. D ~5 b# I* q' S
程序代码' m4 A4 A u( D* X' U$ `% @
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素8 o( ^9 s/ L5 M5 ^4 D( Z! g' R6 J
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
7 Z2 e# V+ ^/ C4 x, q: f: T( v% v3 S: I. ?/ B$ x3 M
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
C* h9 d; B4 `' y1 L% v/ C& e$ I% G6 Q0 s0 C; Q; \
程序代码
+ I) K/ D3 K8 U/ V6 F/ C% Istring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
, @' c2 F) h1 s3 estring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素+ j% E9 ~$ |* j0 ?6 j& e J
0 o2 S* E5 }0 O. {3 [7 d `- j6. public string[] Split(string[] separator, int count, StringSplitOptions options)/ ]& J& H- X$ m$ }2 m
! y4 _. q8 W, p+ `" p; c* c$ s; P代码+ E/ Q* a+ h: G- \5 m0 u4 S
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素! n! v! g. @0 b. |- C/ S
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |