下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
! Z- D4 | {8 y8 Nhttp://www.itwis.com/html/net/c/20100506/8234.html
( n0 L; C6 D( \0 m, t) T0 t* B
0 Y' ~* z! q+ @2 R( J# S程序代码3 y& e0 P1 a5 u) y
1) public string[] Split(params char[] separator)
2 O( O7 c/ `" u( [2) public string[] Split(char[] separator, int count)
$ S' O; |4 U) a: M& d3) public string[] Split(char[] separator, StringSplitOptions options). k- z! t- ^! p
4) public string[] Split(string[] separator, StringSplitOptions options)% H K0 g) {( L5 D/ Y1 K
5) public string[] Split(char[] separator, int count, StringSplitOptions options)) P* W6 F1 u: u4 I- r/ `6 U" w/ B
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
/ e9 {4 c% Y0 G1 L+ }6 v
+ t) j9 g2 g; i( p2 Y下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
+ u7 q( ~/ j8 |. J
0 t6 o% @5 u5 m' A8 T) w1 \0 ?1 i. a1. public string[] Split(params char[] separator)
7 c, g$ X6 W! P
, n7 y% ]& v# N* s& n 程序代码9 ^. s0 [- r' G% {& ]$ w0 O# o. p: I- N
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}" j- C5 h* Q" x) B6 I: F% z
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
4 a2 K# N7 o. J( b0 d% L
' n; I, f1 O1 L3 |2 s: ~2 h2. public string[] Split(char[] separator, int count)# W2 S1 t' y" `* K# x' D) ~
, y/ L: t% t5 ` | 程序代码3 A9 {, T" }+ H8 G% }3 n( Y1 Z; K
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
' k# p |1 M( T5 z6 w# ^8 S5 Astring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}5 U" p( m" C5 W0 i) C3 N! C" l4 o
! s' P( \$ x8 S" J/ W
3. public string[] Split(char[] separator, StringSplitOptions options)
1 D3 x* {/ [. T6 B# @6 o3 {; D3 w a6 k0 E9 s
程序代码
" O" e4 l! D$ d6 t7 z1 \string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素$ ~& U4 I: ?: ]
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素5 r! b! r! u0 n* V- x: S
: Q. r" }0 h. k+ P# d; p4. public string[] Split(string[] separator, StringSplitOptions options)# K" i. ~3 v5 F, Q, d* ^5 u
* y* J2 W8 ?% B' b+ G. m
程序代码
! \) b: `" x: M4 fstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素( g% E* N! e% N; _
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
; [* g F& h9 @0 Y M6 I' v4 |' {* P
7 s& {- G" _& r- C8 N6 x( q5. public string[] Split(char[] separator, int count, StringSplitOptions options)
" e( o1 Q; `9 K+ A' L: n1 i
, L& Y8 q/ w# j. z% M; N; Q 程序代码- Y/ l( S: {# }6 I) ^& [9 O3 W6 z% f
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素9 ^- j* S9 t3 g( Y5 L S$ X
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素3 N' D$ z$ \7 I: K# G5 m- U
( a% q; Y+ c8 q: F b: @6. public string[] Split(string[] separator, int count, StringSplitOptions options)
' B. l1 Q3 @7 S3 {- X& h" B& a$ q2 R9 }9 o! g' h9 \
代码
1 U; r# t3 e6 B" }string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
; S. ~$ o8 X8 k" Qstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |