下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看
3 B7 x1 Z* k$ P( E; g* _$ Ehttp://www.itwis.com/html/net/c/20100506/8234.html
$ P# P: u2 y2 V6 F0 |9 J$ z) N8 {3 e- ~9 n9 C$ X
程序代码
7 @% n$ Y n* g& a1) public string[] Split(params char[] separator)* ?: _3 F5 l: a/ e' E# f- L
2) public string[] Split(char[] separator, int count)4 E1 W L% t1 M9 x* D5 D
3) public string[] Split(char[] separator, StringSplitOptions options)
# q# T; k$ M+ ]; n$ y# n4) public string[] Split(string[] separator, StringSplitOptions options)) L, U$ u. S O
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
- h& {: {& g% i. Q% c6) public string[] Split(string[] separator, int count, StringSplitOptions options)2 ]& [$ d. ~9 _7 {+ B8 f* ~
3 F! z" d( D8 b, p下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):( Q1 O1 t+ v0 o, p
4 G1 W7 Q% `4 V7 c- _
1. public string[] Split(params char[] separator)
8 ^) K+ D& s0 B$ _ q) H0 Q( K
: y! m( k$ [9 q9 F& l5 n2 q4 s/ \ 程序代码. k1 g. R0 Y% k
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
+ C8 n& L% o5 F- ~string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
1 @, v! S; `, r' p. V- A9 A6 u: l$ o. j6 P' k
2. public string[] Split(char[] separator, int count)
0 F3 [) |; b) q, k) g! |' T
! s }; O( V/ `! ] 程序代码
) W, W5 O1 E2 v- K& S( m8 Hstring[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}# K, X; t5 C y
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}! @8 S- \" G1 h
8 O9 v8 v8 j% f( o% i2 B+ W4 R7 v
3. public string[] Split(char[] separator, StringSplitOptions options)
2 N+ V9 }1 w7 R {7 v [- b q6 T$ t( c7 h
程序代码
: J2 X4 p& ~( Ostring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
9 U! Z4 ~9 r! F2 U _string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
+ `) o" Q$ _4 w4 ]+ g# m q) Z9 D V0 X
4. public string[] Split(string[] separator, StringSplitOptions options)
3 E8 y' L6 X+ w' j: h- |" h0 |8 g
8 V. T, N# X; Z& l$ \# W 程序代码
; Y8 l3 {( r* Kstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
0 e# [" [4 f* C; s! |6 ]! ?3 kstring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素2 W5 G3 P8 o8 |* N! V# O7 v
. y) n: y3 ~& a. i
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
" l2 o3 c9 ?3 c9 _# e* A6 |# X1 q, T& ~' V
程序代码
$ M3 z& L! u+ q5 m P* ^string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
0 f4 v N# E+ V5 S' istring[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素3 V( F8 v/ U5 N0 |) v
8 b1 t* }+ F1 Q6. public string[] Split(string[] separator, int count, StringSplitOptions options)
7 Z5 h! i6 W0 h- l6 z* X3 w$ P# s& J8 s
代码% z0 ^7 p! S3 A: o
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
# v1 N- q! V" X6 ?- C% Jstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |