下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
, D7 v2 o& S+ d; J3 {$ ahttp://www.itwis.com/html/net/c/20100506/8234.html
3 A* r& i: w7 S2 V2 L* H4 p
7 K3 c; G9 ]/ p程序代码
6 j0 _; L% [9 ~3 v- g) y1) public string[] Split(params char[] separator)5 M8 |1 |1 F+ d+ D; t- T6 H
2) public string[] Split(char[] separator, int count)( T/ G0 ^( t/ N
3) public string[] Split(char[] separator, StringSplitOptions options)4 P0 o, N" P( H; U [
4) public string[] Split(string[] separator, StringSplitOptions options)% U7 k& }; P/ ^6 ?2 ?
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
E5 `$ J% [/ c7 v1 t* D# U z6) public string[] Split(string[] separator, int count, StringSplitOptions options)
# r8 ~! {7 Y7 \ D# E1 p7 H, N' G+ F1 t( S2 f4 q
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
. k6 u. ?4 D9 ?) Q/ x) n, K7 U# S$ a7 p9 ^, X0 [ d+ y
1. public string[] Split(params char[] separator)3 W$ Z' U- ?! {; r) l. a
$ U7 }3 e3 A+ T# W( f6 c6 d. c, t1 b 程序代码" k9 W& @, x- b& ~! M2 {) W
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
7 r2 y6 K' G7 D# Estring[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}- N9 g8 O: |( V& m
0 _( S% S8 U$ ?. ~& n2. public string[] Split(char[] separator, int count)
. G# r/ `' V* @2 [# }4 b8 i2 W
2 f% k9 d/ A% l' w' S, H+ [ 程序代码
& M- B7 [9 @9 E! J# G7 I! f3 Nstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
2 C& U' s' f }; bstring[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
9 h+ Y8 V; U9 X- X! I; [/ O# ~" `; \- n" d+ w3 y
3. public string[] Split(char[] separator, StringSplitOptions options)
% V. F: r& G% D# s$ ~- l3 f' k- T+ H+ y6 _3 U; u8 A5 V$ {
程序代码
- ~; I* ]! }1 T$ G/ ]! Kstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
5 E' X- w; F. u5 P) ystring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ A4 v, q4 F* i
8 F! D3 Q8 K( `' k
4. public string[] Split(string[] separator, StringSplitOptions options)2 t! m, @5 p8 e! g) b5 V
. ^+ p. }8 i3 Q: ?2 E
程序代码, Z" V8 X. @7 e( J# n1 n* R
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素' \2 j* r: }" O
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
) `( i5 K) R# z: L" P) @' q; F& |$ @% |5 s& i# e: L
5. public string[] Split(char[] separator, int count, StringSplitOptions options): {. Y0 G% C/ K
S. A! o% A0 |" z3 Y9 s 程序代码# h) r2 K! b# ]) k/ {
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素) q7 _. k9 ~4 q' S$ a
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
' W& \0 S. s9 s7 i: ^3 _
6 r2 C) h* b3 f9 M$ b: [. Y6. public string[] Split(string[] separator, int count, StringSplitOptions options)% H; h6 j- f L& w: v: K
! F7 H3 @ w/ {: L8 }. u2 R8 c
代码
) s6 V p6 m# ]$ Ystring[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素; E M( A- ^! P) X1 o8 T8 Y
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |