下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
8 o& G7 |" _7 t! y/ E1 X {http://www.itwis.com/html/net/c/20100506/8234.html
) \9 ?% U# v: \, I! M: [7 ]. q, }% N/ _
程序代码
* l& y* }# n+ p9 u* @1) public string[] Split(params char[] separator)
U! y( }; j2 o% D+ d2) public string[] Split(char[] separator, int count)' ~; w {5 `+ K1 A. L5 ]
3) public string[] Split(char[] separator, StringSplitOptions options)% E$ y; P1 f9 C G# r* ]8 T0 o8 U
4) public string[] Split(string[] separator, StringSplitOptions options)
$ F S/ m7 T3 X( f5) public string[] Split(char[] separator, int count, StringSplitOptions options). @+ f/ D8 \0 Z! C9 M+ m
6) public string[] Split(string[] separator, int count, StringSplitOptions options)" R0 [+ v0 m& n2 B( m5 t: h$ U3 h
" H8 [+ ]8 L' K) \- w6 c& a( W/ q
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
- \$ H+ C5 L6 M; M6 [1 b/ |1 w* q8 n
1. public string[] Split(params char[] separator)" D3 f9 b* t1 K: |3 m
$ O3 I% J$ A, b# i! J' G$ ] 程序代码
! Y8 Q5 m/ Z9 @: b$ W' Kstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
5 H1 d. [8 b4 o; A" T! Qstring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
; |2 ]1 M% e3 b. j! B9 m) @! q0 k6 K) W# {6 n+ ^9 Y
2. public string[] Split(char[] separator, int count)
' ^: e! e- R# ~' U9 \( H
& o+ Z9 r% a/ i) `3 s$ p1 W 程序代码9 ~/ J) X+ J1 m+ g0 f8 n# c
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}6 L0 ]0 ~% `6 s% x! {
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}# y8 t/ y7 ?7 B9 k
' K* `$ y J/ c7 y$ h6 j
3. public string[] Split(char[] separator, StringSplitOptions options)
+ V& [9 o7 x9 _% M$ `' G0 C: {; D0 ]2 k
程序代码
( v) Q' ]$ X/ f+ _5 T+ D: Fstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
; V" Q8 t. {. a# F% Q- C3 dstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
" V0 |5 D) U4 h4 J: [: v2 U# c$ a6 z: r
4. public string[] Split(string[] separator, StringSplitOptions options)* i! I! a8 m9 N% `3 }
7 m* g I9 b: B# n# x$ @
程序代码
/ h |& X' b N0 Sstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素6 s, w- \8 D# ~, `
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
% d5 @, x% t/ {" x! l1 d6 D) `2 f- X( o0 ]/ |2 d
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
9 f# t' R6 v" N- u9 V# c* m) t: t. F; z
程序代码
( U/ j3 S; |+ K( \) Dstring[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素( O! g7 ?( f! @. j. ^4 t4 X
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
& h4 ]. J( h G& j4 F8 j
' ~# d! I: z) I+ j/ a1 T6. public string[] Split(string[] separator, int count, StringSplitOptions options)
7 |5 [- X' M2 t1 y/ @' b6 ^, t, o- o( g
代码
1 k5 i: n8 f' s( wstring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
( G5 t; ^/ [$ c9 j% Pstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |