下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看9 F P, U* f) E' ~5 E, X4 N; U
http://www.itwis.com/html/net/c/20100506/8234.html
4 b) N2 L P& o W( ~1 S# a9 V9 n. J2 h2 \' B
程序代码& @: Y5 A/ ^( k6 U
1) public string[] Split(params char[] separator)* I, c8 U. {5 e, ] B. g/ h8 M/ A L
2) public string[] Split(char[] separator, int count)
; y, L i7 t! l8 z3) public string[] Split(char[] separator, StringSplitOptions options)
U+ N5 }- r0 t! W9 V7 O, S4) public string[] Split(string[] separator, StringSplitOptions options)
( k) ^' d7 q( Z$ s! [5 H: F5) public string[] Split(char[] separator, int count, StringSplitOptions options)) @* R* n% `3 t5 R* c1 v7 m
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
: Z) C8 H: d; o* q" o& r1 X3 y& k# I2 k+ ]' K! \, \
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
. Z3 g( P- Q* J- m9 U( O! h. c3 `( h5 r. D# N1 t/ p. b! B
1. public string[] Split(params char[] separator)
& R% I9 e" f- d$ ^! w8 H- \( T4 j' d! d" H7 i
程序代码0 h' C5 ~) v* L/ X3 K* w# r& M) h8 B. O
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
8 g2 C1 u( c$ R( l; B; Rstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}4 V# n! U, Q8 m. {
% N- V0 Q# \# y0 X: z O2. public string[] Split(char[] separator, int count)) @: a) R2 r' Q3 f2 O- C8 l- U
& D: }1 \; D! J4 A K7 R 程序代码
, S5 p' ~/ u1 Z* M; v) ]8 i' Zstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
3 E1 P9 S7 l1 M8 k3 fstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}- P* ~, q/ _7 Y/ M5 |
+ h- {0 | k* s! H4 D5 k
3. public string[] Split(char[] separator, StringSplitOptions options)" @/ D! @6 p: d. K% B" e$ Y
5 G; b- d$ ?" x: {) @% F
程序代码7 @" Q8 o) @! h" v
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
8 Y* B' y' J6 I2 Nstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
) }7 |& i! U3 [( R* W5 |3 e+ H& z3 g! Q2 L" a
4. public string[] Split(string[] separator, StringSplitOptions options)
- ~' b+ x: n% B# `9 z d( n% c. N1 C a
程序代码5 u8 N# D' X( L2 e8 t+ B/ m4 o
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
9 f/ {- o3 ]) e& g/ D# Xstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素+ L6 p, v9 h: l; F% }
1 {" J' y4 l5 N* e. \. z2 L5. public string[] Split(char[] separator, int count, StringSplitOptions options)
2 V$ S2 e( |" ~. ]% K- }$ k* i' k$ l4 P- Z) b) C, u p& B
程序代码
6 | I$ @- N8 }& W k) b# Fstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素9 I+ l! b3 |( O; F C
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素% S4 w+ c' [0 i$ U6 B5 y
$ g+ D& `3 h& L1 c5 c2 u6. public string[] Split(string[] separator, int count, StringSplitOptions options)
- E% E8 E5 c* r) I
% ~* M. T/ P$ W) l代码
: k" D+ ~+ W0 s7 r' L7 qstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
0 z# E6 `' x1 M' N3 k- e7 mstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |