下面的这篇文章,介绍了Split方法的各种重载的应用,并有例子,字符串切割的话可以看看 m* Z" \" D; M% a# _) p# v
http://www.itwis.com/html/net/c/20100506/8234.html+ K$ c# Q& D0 b3 \! r1 {; p) @
1 J+ Z5 Q& s! J. _* i# `
程序代码, `: B! R, U, {, A8 w( r
1) public string[] Split(params char[] separator)
! {% s1 z) ?! ? ~- ], q" K2) public string[] Split(char[] separator, int count)
- Y, ]' y' P% s: f* L3) public string[] Split(char[] separator, StringSplitOptions options). Q% [' Q; Q1 |' P9 ?
4) public string[] Split(string[] separator, StringSplitOptions options)+ X& Y( U5 n- A9 _& ? L0 U- T; M1 n
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
; t! h$ X6 c( j1 M; @9 w6) public string[] Split(string[] separator, int count, StringSplitOptions options)
9 u" l3 Z# p: J/ g, r0 ^5 \, r4 v+ H( e f- N5 i! f; `) B6 x
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):: }0 P+ T4 }6 q4 @" j5 F
8 y* O8 V, v d) ?- x1. public string[] Split(params char[] separator)8 a% X- ?6 K: `9 Q
5 p+ O9 i! g: t( R1 e9 [4 g 程序代码
; b; f( ^) }) i& {7 W8 {0 xstring[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
; m7 q, b: a2 `string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
, U+ s9 v" _! M5 `
E( q7 O) G# {/ J/ Q) O2. public string[] Split(char[] separator, int count)
2 m% T3 X3 t) i* P0 d Q0 O% H, M8 u! F! W5 u1 s/ k1 r6 Q9 z, x* b
程序代码% E w! ^9 b' a/ A% {: H
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}% U1 D& y* q4 F5 ^8 Y! \
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}. Q7 ?8 O, C B: C" l; r% t
0 D% U+ N8 v3 Z* V7 h2 M- `* ~3. public string[] Split(char[] separator, StringSplitOptions options)9 [4 L# Y( p6 N1 M' W
8 e9 O5 i" {- G L7 O 程序代码
( D/ I) R5 v) Z) Cstring[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素7 |; g' n: a0 O; {
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素/ z2 B4 ^) V# W& L3 Z. p0 _
8 K/ p8 v$ j6 ~4. public string[] Split(string[] separator, StringSplitOptions options)# z# }) w$ T/ p+ z5 J
% f G% u0 h1 |+ P& l& g% {
程序代码
* Q2 Q, J: S* P4 x" d( Estring[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素; {0 Q j& a+ \) o& d3 q
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
" r( O1 O$ C- B- O# B
) D: v( F0 O6 I1 Q+ k0 k5. public string[] Split(char[] separator, int count, StringSplitOptions options)
7 k; c5 k6 _9 D6 `2 u6 n" z: c
$ r1 j4 W% }) A( x' p( ` 程序代码6 H* z1 y6 @5 F- F) O3 j
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素8 J, `9 S- C2 G6 h: c( G
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5 v9 b4 w/ ?' {. O; i' m- z# T; V' ^
& K* V1 o7 I$ w6. public string[] Split(string[] separator, int count, StringSplitOptions options): u. u& q2 Y2 y: N* r' w
9 v2 ?' e9 E, @0 n" r
代码 S8 h0 R3 k H3 W7 }# ?" s1 d/ y* m
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
$ w- o: x! T5 s) U' P; Q6 C2 gstring[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 |