下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看- c0 I4 i. F9 N4 q0 G
http://www.itwis.com/html/net/c/20100506/8234.html; x# a4 u b" z: `3 R
$ u3 K- _" M' r' A. s% O程序代码1 v9 m% a$ k; H. g* \' D1 t# N, S
1) public string[] Split(params char[] separator)" A. y5 M# z5 K1 ~) {' A% E
2) public string[] Split(char[] separator, int count)
- S2 t$ z9 q/ R1 \$ U3) public string[] Split(char[] separator, StringSplitOptions options)
/ M! b7 ~! y2 m5 P- f/ y6 Z4) public string[] Split(string[] separator, StringSplitOptions options)
& R8 A9 z4 I" l% L5) public string[] Split(char[] separator, int count, StringSplitOptions options)
$ q$ z) e$ f- e! }) x2 I4 L! Y, s6) public string[] Split(string[] separator, int count, StringSplitOptions options)
" [ _9 J& Y7 x1 H3 E( o% p9 i2 P) i" O% v3 A( j
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
+ v1 A5 g% i, f- _4 |5 w& b/ \4 D7 E
1. public string[] Split(params char[] separator)
! O7 p( h2 C& T6 t: V+ b. P6 H7 P
0 G2 m9 }# e: ]8 ?7 w- _ Z! H 程序代码; V- R* l. O3 Y% K0 w
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
1 V- S; i/ F+ zstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}; r- y% n& C4 b7 g% ^7 j2 M
; X Z2 |" m; k
2. public string[] Split(char[] separator, int count)- b' F- H, |% c
$ f; ]3 c* q: v+ ]$ n2 v" I& W
程序代码+ `+ N- U" F! i; t; e
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}8 I2 L5 ]2 T! N2 Y8 P
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}- p# o4 E( k4 L- j4 v
# _1 |! v9 g# X) {- S7 C
3. public string[] Split(char[] separator, StringSplitOptions options)
- [4 x6 h6 J2 C' G5 p( y3 n' X& U, Z) T- j4 R i
程序代码
. i9 M3 O0 A7 Kstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
; t: y s) [: j$ \5 k" c4 _string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
* f! x# v* J% R" t4 x' q3 o( ]* Y: e- w1 n
4. public string[] Split(string[] separator, StringSplitOptions options)' r+ {0 w/ q7 F0 ]4 G; h
- g+ _' v2 ?* s1 K9 N5 D/ k 程序代码% j1 N1 [8 f$ I% N/ I
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
% s5 C& ]4 o G+ c. hstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 ~1 ?: \- H2 J* J$ U# e: ^* v0 m
) D* E# g. w, o3 z# ^5. public string[] Split(char[] separator, int count, StringSplitOptions options)% x$ K( D6 C5 r/ L& V3 X
0 F5 S' h' ]/ m) j
程序代码1 T7 }; H+ \ K& @0 a
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
1 p: a6 N- _( P) i0 W- R# Lstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
2 V1 W6 Q/ T4 F1 o( g8 C n- {, O6 A# ^5 j! i$ n0 s
6. public string[] Split(string[] separator, int count, StringSplitOptions options)6 q% y0 l$ P. d
9 O0 q- N0 z9 E- |' f5 t& L4 `代码6 O7 \/ \/ |7 n
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
8 [& `8 @! s/ r+ u. Hstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |