下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看4 ~. F$ n9 b2 v& b6 j
http://www.itwis.com/html/net/c/20100506/8234.html& Q) `8 k9 ^2 R" ~2 N
3 a* X0 f9 r' m
程序代码6 U2 ~: j1 X4 i* g( T8 f
1) public string[] Split(params char[] separator)
3 M6 H( \3 W1 g2) public string[] Split(char[] separator, int count)! U: K9 j8 j8 B0 T4 }
3) public string[] Split(char[] separator, StringSplitOptions options)
7 N# Q% W& y! A4) public string[] Split(string[] separator, StringSplitOptions options)
" _5 X$ V' p- J$ I$ F9 @* ^+ T$ w5) public string[] Split(char[] separator, int count, StringSplitOptions options)
& D$ N a5 f: Y# q5 i6) public string[] Split(string[] separator, int count, StringSplitOptions options)/ n5 Y- m+ { [& i1 r+ ]; _" l
2 L$ D2 f1 E3 a( L0 i* `
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
/ y7 w- |3 V5 f# y% @
4 M( `( B3 C0 a7 L8 \3 _6 t4 m1. public string[] Split(params char[] separator)
7 M' r1 ^1 V' L: N3 x* [1 U+ W) P( W# d6 q5 R8 }- K. G
程序代码
' A9 G- j. U& C, Dstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}2 G$ @5 U! W# ?, x- _
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
! T9 I1 Z2 L. v
( C' }/ O4 ~# `5 X2. public string[] Split(char[] separator, int count)4 F; F, N5 o' G% i% D" r
9 W- W- s' O# ~2 X2 Z* Q* c 程序代码
9 ^" U5 Y' ~' c m- u/ [string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}9 E* }" ?* Q/ S% T& }$ U
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
" m6 D* V7 d. {, t! _2 ^2 R
7 E/ E9 l; B9 \* Q& a3. public string[] Split(char[] separator, StringSplitOptions options): U2 A( ~# Q; e3 w) r! x! B0 P
- b4 H9 R- H: I' l' f! A) _" J) X( I5 H 程序代码) }6 G- Q" V4 _( v# M, U
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素% |$ V: w5 s \7 H3 Q! v1 H% s* {# D% n
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素3 Z0 M( [8 s: V; y/ l6 g. j
" g* M, l2 B; Z; D: e+ z, h$ n- D
4. public string[] Split(string[] separator, StringSplitOptions options)
- `6 z9 K) g6 S* |. W. \1 c( t; J, P
程序代码2 o( {$ R% H$ @# J2 x2 N
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
4 A9 M5 j$ N5 i( J0 l5 `* U# v1 |* _string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素+ X: w+ `- E6 B' r" u
" k8 k9 ]' ] a: G7 F. _8 t
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
* B9 e0 p: l, Y/ M* X u. n
( {) s2 p2 ]! ] u3 m 程序代码5 l5 N, q$ Z/ k8 D6 Q; i
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
) ] e6 _% @! R1 G/ ^; S8 H4 wstring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素$ [8 D6 c5 H9 b' w; C1 B2 n0 c. F0 _
8 r3 k1 Z1 L+ _9 B& S
6. public string[] Split(string[] separator, int count, StringSplitOptions options)8 B# G$ a8 }* T* e' k& d
: p1 m b: {$ G3 Y' K/ ^代码, ^( T4 N7 c8 ^; X2 X
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素) T* t# h E" T5 ?! i
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |