下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
1 [) E7 e9 o( l e+ V5 P, |http://www.itwis.com/html/net/c/20100506/8234.html/ w. d. Q% E& X0 k* R
1 V9 V$ b) [" u
程序代码" ]: O+ z$ |: L1 M5 r; q/ E; y1 k
1) public string[] Split(params char[] separator)
% {: t" V: G- R" I* A2) public string[] Split(char[] separator, int count)* [; @$ C2 i+ a5 Z! c
3) public string[] Split(char[] separator, StringSplitOptions options)
/ H3 g# ?3 c5 M* a2 I; J4) public string[] Split(string[] separator, StringSplitOptions options)
3 {4 u' @ U6 L( u5) public string[] Split(char[] separator, int count, StringSplitOptions options)8 ]0 b* V! V) o) Z
6) public string[] Split(string[] separator, int count, StringSplitOptions options)# G/ E! @% R: ?/ f2 |* _
: e# ]6 M* l4 B: Z! x
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
4 E5 `" f8 ~1 G. F! r& |
8 G7 |. M% a! A' y# u9 p1. public string[] Split(params char[] separator)* C: E. B6 z' B# G* q: V/ T& ?' F
, s3 h3 _; @9 ]/ G G8 L
程序代码
8 r; ?* m1 @3 n# \& ?. P$ I xstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}, c, M, m5 \; D; V! \1 N4 X
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}+ p, q; g3 i; ^
' g2 \! @, l" E1 X8 l5 C2. public string[] Split(char[] separator, int count)
" P6 L; \8 C# \) e' S0 ?: r' k" R% D% X# A1 Q. P4 E! R3 |
程序代码, k4 a- e& n! @3 S: ]
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
7 y) f" E" @; Z/ u b$ dstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
0 n: [+ B! k7 z: w, S G! L- L$ ?' k' j" V/ x( Y2 b p9 O4 q
3. public string[] Split(char[] separator, StringSplitOptions options)
: Z" ~1 |/ U- r2 o0 f2 ]+ Y, K1 J/ P" u* _; U) \/ p& q# ~
程序代码; {1 c2 M7 W5 c. E: B4 \
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
) i* c: { ?. a/ K* Y [0 rstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
$ x# g o9 R" C* ]3 f$ `- ~6 q% S* S
4. public string[] Split(string[] separator, StringSplitOptions options)( B2 r8 d9 y9 z' C; Q. x) n" |
( M4 P% v" N5 n% q7 ]; L: C
程序代码; G1 W* R5 l' o" `, Q5 w- J
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
: e5 N( u: t. [3 l( o& X* istring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素; Q* g+ z9 o6 S! X) v- I+ Y
' m+ p9 m6 o6 i& \4 C9 p9 V; ~
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
" X1 j5 ?/ E$ s) P8 J' [; s- l$ b' D9 R% \2 E0 @8 V/ \% ~
程序代码! ~4 c; g! o5 d- a l
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
6 v; J8 g* k7 f! N B( v6 lstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素- x$ B! d6 F! Q. \7 z: H# ?8 n, e
- i' W; \. |( B: q @
6. public string[] Split(string[] separator, int count, StringSplitOptions options)- L S- a* u& ? i9 R! _7 W
2 Z1 v" l0 x2 h0 U" b! ^
代码+ t9 n3 E2 Q2 s7 j! \
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
8 ] C, h, o9 x# e, c0 Pstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |