下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看+ f) t. o6 Z' k$ Q& U6 @7 ?
http://www.itwis.com/html/net/c/20100506/8234.html" p7 b7 N# K- b- d' ^
5 J! x& ]; d/ p% l4 K, ^. l/ ~8 B
程序代码- Z5 J( ?4 K; A7 _5 X n
1) public string[] Split(params char[] separator)
* @$ {5 D6 V. @2) public string[] Split(char[] separator, int count)2 d( k4 h8 Q* x* m
3) public string[] Split(char[] separator, StringSplitOptions options)7 Z# N* f' U9 K
4) public string[] Split(string[] separator, StringSplitOptions options)
$ [. I% C: E! J9 f2 @; P5) public string[] Split(char[] separator, int count, StringSplitOptions options)
, a& C% f) l. z% j1 e6) public string[] Split(string[] separator, int count, StringSplitOptions options)
! E- r- S! D& \3 N* F9 e/ v
, ~# ~. B0 ?' _) n2 M# f8 q下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):2 c3 F% t" F% ~; R
& m' r4 q6 g2 C/ N- J& G
1. public string[] Split(params char[] separator)
" N5 S8 w2 W; p O9 I+ b
2 ]; O8 y- L1 ` 程序代码+ P) d* L3 t/ J4 W, u
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}0 s- I( n8 E, a/ ], k3 i( r
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
% N( Y( {' n/ o* r8 k: j
1 f7 j3 D0 f$ A% c7 @& ^5 T2. public string[] Split(char[] separator, int count)
+ o5 v* U3 Y9 N9 t. S8 m2 `$ a* J0 o) ~
程序代码$ _; {, { E' a- \. a
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}1 y. ^. j2 |7 F2 A3 z) A
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}) a `% D) M0 x) B/ I
% l9 Q( `5 v3 B) @3. public string[] Split(char[] separator, StringSplitOptions options)
3 T& O% T% u$ }1 q) |6 ? a0 x4 |% Y
程序代码' l: ^& l! d# Z( Q: I5 t& x( h
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素2 t9 h7 n: k3 I0 c
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5 N; m0 u" h; P' v/ K- x2 ]% B! Y9 V
$ F4 k9 y( d. u2 ~4. public string[] Split(string[] separator, StringSplitOptions options)$ t {4 _# z& y" Z& @
5 S' b/ o6 f) L* U7 H5 K
程序代码
' m9 A2 U8 [+ s: R! estring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 y$ F% x# h1 c8 F" Y
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素! @ f* @8 z+ o, [6 N
1 c0 ^0 D- S z7 Z4 M' k5. public string[] Split(char[] separator, int count, StringSplitOptions options)4 B% c2 S/ G( M8 f
" j' z- X$ l! ~; u( U$ A) ?5 ]0 Y
程序代码
8 a$ D M& e* X+ y/ |5 J( I+ P* Nstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素( P. \( T6 ~( [, |) t" ~: L
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素1 Q, p* a' O) f7 p
+ o) C0 Q4 Z4 {1 Q* E9 X
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
( m& ]9 t! o% G
* i. P' b, t1 u代码
, i: p3 D1 h% r! H. q2 E( P! Jstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素5 d) {8 W, B1 x. h
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |