下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看2 ]- f9 T" ]0 ]* c! c, D% }
http://www.itwis.com/html/net/c/20100506/8234.html
+ B5 G. T( O9 {2 c r7 N+ P2 k4 q) C7 I2 P
程序代码! @' P. t: L% H$ \& b$ q+ K0 \
1) public string[] Split(params char[] separator)8 r* p$ Z9 `: x
2) public string[] Split(char[] separator, int count)2 T5 y1 V; [1 w$ e+ {7 W9 k5 J
3) public string[] Split(char[] separator, StringSplitOptions options)+ N# s( _" ~% L, j% x
4) public string[] Split(string[] separator, StringSplitOptions options)) s6 B/ K+ ~- U/ l& R* e
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
+ i, ~4 _' Z, O6 g6) public string[] Split(string[] separator, int count, StringSplitOptions options)
# E" `: H2 V( ?' e/ Y$ N7 K6 ?& ?
1 U5 G: _% v* B. T下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):. l$ o$ b4 M2 C
% I8 D/ b5 Q" k1. public string[] Split(params char[] separator)) ^" ]3 J; [0 I% ~* g7 j7 ]* F+ P
+ q: Z1 B0 _$ m" W- Y 程序代码4 H/ d8 v3 S4 l0 E8 W5 i# N
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}% p: _/ q" ?: D1 ?9 i+ m
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"} L4 k7 k( f/ d& `: L
% b" _6 D; M g% r% K: K; T( W: |2. public string[] Split(char[] separator, int count)
" J" l" V$ j' X9 h0 V" K- J+ g3 b7 t" c' \& e0 n
程序代码+ |# h, L2 p6 A+ o0 g% ? K' g
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
# }8 n" g* [2 W/ @. c' @1 X, kstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
/ v7 P- \1 y# _6 ]) l2 \" V
6 B; K; g0 |) v! g% ]3. public string[] Split(char[] separator, StringSplitOptions options)0 z) G4 i' u2 u) \- \, l% P3 f1 s
( O. m/ @( v6 ]4 N c9 N9 g 程序代码
& H& R* l0 W4 V' Y# wstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
# t6 l! K3 }! X$ P& V' j# ^4 ustring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素, C; H3 ^8 P- D' P) z
' z3 l( @1 F! C$ k4. public string[] Split(string[] separator, StringSplitOptions options)* G8 {( M0 ~2 Z) }# O
" W' }* i3 ^5 a/ H8 ~8 F8 A 程序代码* P' t [5 l& O
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
) G& V8 x7 m: k) e+ Y1 wstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素5 P9 E1 E- k. G5 b9 Z1 F" Q1 _
f; G; o* C/ b$ k( M+ b0 E1 `
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
. `) O Y/ D6 b, v C
: t4 B; f6 f" z# h 程序代码
# s4 l: J1 ^+ W/ O% i% {/ F4 [string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
4 X6 Z$ x/ K# B# l' M, G- vstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素' h9 d/ |1 R) L$ D
7 y9 P, w1 n7 f3 m0 Z6. public string[] Split(string[] separator, int count, StringSplitOptions options)0 v! r. \5 t" k* i/ A" J9 |7 O3 C
" P4 i0 v$ p4 k; y代码
2 M& h9 t g5 l. _+ [8 dstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素% ]9 F8 [) b5 o9 S% h
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |