下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看7 s! ]: r" l1 a
http://www.itwis.com/html/net/c/20100506/8234.html
& X0 f7 F: V* T) v) W" d4 ?$ i* _; x& h: G; g6 ` o) E, C- w
程序代码* p8 `- a$ w E9 Q& S; z% j" z: Q
1) public string[] Split(params char[] separator)
( q& `( q* X$ ~2) public string[] Split(char[] separator, int count)( o% @' G4 J u7 h: I) K
3) public string[] Split(char[] separator, StringSplitOptions options)
& ~6 P+ w( q" k4 I! v) k4) public string[] Split(string[] separator, StringSplitOptions options)( K4 i! {' g% `* X9 ? F
5) public string[] Split(char[] separator, int count, StringSplitOptions options)- z( L8 V( y6 I" m7 e. @ B
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
3 M0 L3 f Q. @- I- G; F8 A: Y! g" z) P' X6 i" @, n: t! e4 Q
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):% {$ L9 z0 u- u) f! {
8 H$ Y( E; }: n
1. public string[] Split(params char[] separator)
) }" v: ]6 j- y: H3 I+ R+ d5 _* M) q6 |' s. H+ g. c
程序代码
( G5 h( R( Z8 v9 istring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
! o9 F/ j( _, Z5 cstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
7 V2 A% m/ W* U% U" m- |/ X Q
8 N+ |& l" ]. F5 y2. public string[] Split(char[] separator, int count)
4 [, s8 ]+ L. g* d# l
4 T! d _9 }; z) p N% S 程序代码9 B: S1 Y* }# k
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}; Q7 g! U, M4 W+ D) e, w) y5 n" u' S
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
1 S9 p2 c% o3 w: h6 p
9 J! k8 Z8 {7 Y6 s, O: X$ Y2 b4 n3. public string[] Split(char[] separator, StringSplitOptions options)- C8 B8 c7 a) X+ o
! Z4 }5 p# v% c% u) x 程序代码4 t. v* O4 U8 `' n0 ^7 _& ?" ~
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
% O8 o; q$ B& `- G, w$ vstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
. f$ Y1 q: J& w! J; k: v7 W8 a t. ^ M% s; A1 T' W
4. public string[] Split(string[] separator, StringSplitOptions options)
% w5 J, H+ C) ^3 e! g1 M( d2 W6 S$ B, s$ F1 c
程序代码/ f0 q2 k4 T, N- i1 N# U' ]/ V
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
2 H; q/ [$ r, f' Lstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
( I0 e( c& o5 y; c5 F! }
4 @1 v& W2 K1 m' x, I5. public string[] Split(char[] separator, int count, StringSplitOptions options)
. ~: a/ r3 f/ Z& k- ?& T
. @& D" F* e+ F* {9 F 程序代码
7 i9 b" f3 a+ C6 |0 t Sstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素0 b/ |& M6 q( z" g
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
# s: X N! P( X& W! ]! y* ?" t
6. public string[] Split(string[] separator, int count, StringSplitOptions options)* a }9 b+ X5 @/ F
* _1 x5 o' h9 R8 J' b1 V代码+ ?- `$ X* k$ [% ^( \
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素1 |7 E6 u5 E- j1 i
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |