下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
$ u f8 R) z# i/ r$ Lhttp://www.itwis.com/html/net/c/20100506/8234.html( ~8 E% b6 J' ?& Z; [
/ H+ e& c8 r9 X. O; {* i
程序代码
+ e, x9 u" k1 B" o% e3 F1) public string[] Split(params char[] separator)9 r9 D: C- I, ]5 n! V: T j- `5 ^
2) public string[] Split(char[] separator, int count)6 b- n0 c1 g. Z" }
3) public string[] Split(char[] separator, StringSplitOptions options)- A! M9 Q. ^! U p0 K+ r! u
4) public string[] Split(string[] separator, StringSplitOptions options)
& G f6 P# A. o5) public string[] Split(char[] separator, int count, StringSplitOptions options)+ K" C$ Q2 |( {2 C# D
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
K8 d: E- C! m. A/ s0 s% G) |% W9 \" Q; C8 o
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
. C) A, q. M7 T
F/ T! z$ Y6 `( l S2 k1. public string[] Split(params char[] separator)
l6 e. {8 i9 g; f* ~
+ h$ R8 C# f2 p8 ] 程序代码
/ @/ ?# |# }; Ustring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}& {3 k6 J4 r$ E, B% M, h
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}; W/ O% y3 ]4 A! a1 V" g3 p) y; O
: Y; {' j8 W4 N/ p z! f b4 B% M2. public string[] Split(char[] separator, int count)& [- `2 n( e. a! s1 y) J' R y6 ^
3 g, l, |, H7 \ s3 h$ Z/ l c% E
程序代码' ^2 C1 F! b8 e1 V9 J" f9 {0 g2 \
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}) N" @) ~. U; r* T0 r) Y# S
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}$ O5 a6 P& y( p5 j5 E$ x# h
d3 Z V8 i7 [, Z
3. public string[] Split(char[] separator, StringSplitOptions options)
' z' y! C7 Q/ k2 _: b" u
" o/ W" h+ T0 A4 v 程序代码# R" H( ] W4 P1 w3 {
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
. M* v+ S8 g( g5 l& @string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' ^1 j; K7 Y2 G( {7 w# Q1 O2 _$ t+ {( h7 z& X3 M
4. public string[] Split(string[] separator, StringSplitOptions options)( y( c, V9 i0 I S) e5 z% q
0 b1 Y( Q b( ~& c 程序代码5 {/ x& O- V V$ P/ i
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
8 {* x! }; x. k1 Sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素8 E/ z0 B! y& r& R$ X
! v k9 T0 U5 J# [5. public string[] Split(char[] separator, int count, StringSplitOptions options)* i3 ^5 |5 v8 y8 Y
+ q6 c) }& s0 d/ D! e1 o 程序代码. D, l5 C9 k0 f$ f2 b1 o
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
2 ]. a5 R6 G! K& j9 o0 O+ A, c( \string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
4 k% }! I3 e l3 T' z9 v; T4 B$ q9 Z6 F, E
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
+ U: W/ E& Z& N/ k6 Q% Y/ v+ T) h
5 z3 f2 |, p; c; |( T! U8 |1 n: F' C代码
. W2 t2 T6 `3 p3 A+ s( y( J! hstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素$ } a5 t) @% q O4 y
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |