下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看1 I( I$ ?, ]. i) V4 t$ Z8 M
http://www.itwis.com/html/net/c/20100506/8234.html
/ Z1 w: H! {" W' y! w' M! j- r+ T4 Z# J @
程序代码! v$ V9 J- O- i6 i# N
1) public string[] Split(params char[] separator)) u" X! s( L7 x0 ]/ z9 R. d
2) public string[] Split(char[] separator, int count)
2 T9 u' q: t! A; Z- e3) public string[] Split(char[] separator, StringSplitOptions options)
6 t/ |' i/ @, S% H9 Y4) public string[] Split(string[] separator, StringSplitOptions options)
* [$ v/ c$ |9 }$ F, \! @" J8 ~5) public string[] Split(char[] separator, int count, StringSplitOptions options)
; @' Q. v3 N5 L0 m6) public string[] Split(string[] separator, int count, StringSplitOptions options) d9 c0 N( a) k O D1 E
: V3 X2 Y. h9 q8 f
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):, V3 V2 w/ v, c$ p$ m3 j
* B1 p, D$ t) a! W1. public string[] Split(params char[] separator)" ~0 F9 e+ F4 r0 t$ N
" |( L, l) C' r
程序代码# c3 }' x6 j' i/ Q
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
. I; `, s) d% |( |3 Gstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}- v3 }& [ n0 O
# K2 V) \0 I! u" q! o
2. public string[] Split(char[] separator, int count)/ I; E* u- z' ~# ]0 j- W
! h+ \2 f* |. R$ x3 k7 ?/ O
程序代码3 ~' ~1 I$ i' f: u7 v9 }
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
: _1 M$ \- C) U5 L/ I' A- }0 U1 \* nstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}/ y( R, F; ?3 e# U
/ e, z- i9 N. Z8 o8 u, A1 @
3. public string[] Split(char[] separator, StringSplitOptions options)
7 [% m7 Q, J: n8 s6 P, X1 I. t3 P6 f# B1 E% {
程序代码
5 a) Z7 s" f+ Z# f* ustring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素4 x3 A+ H: L% v# @7 L( A
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
O4 a3 p3 Y+ n, F1 V
& ~% E: Q- v& u- C" J4. public string[] Split(string[] separator, StringSplitOptions options)
+ ~! t. U& b5 i1 Y. ]" Z2 P- X) x% F+ x0 ?
程序代码6 D" T/ f1 i' e8 E0 Y
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素. b' a! i, a2 _: F! w) r# `
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
; O7 G- q% a4 [5 S& K& w
3 V( `& t/ u: r- E7 \+ V! o5. public string[] Split(char[] separator, int count, StringSplitOptions options)+ u+ d* q( p$ X4 h. G& ~" ^
4 A& E+ C' F( g# K
程序代码6 g+ R/ @/ r8 l. o
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
, @' R( x, J9 x' ~4 ^7 x9 wstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
3 d6 l# O$ X. v5 y3 ^. X2 t F! t
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
?' I2 X- n/ f5 e+ v0 H! s2 V. X G" s
代码5 i! F. [3 C5 k a
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# p4 j9 @' a9 ~& [0 b/ B% kstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |