asp.net

C#中split的方法和用法12

2011-02-19

1、用字符串分隔:
using System.Text.RegularExpressions;

string str="aaajsbbbjsccc";

string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);

foreach (string i in sArray) Response.Write(i.ToString() + "<br>");

输出结果:
aaa
bbb
ccc


2、用多个字符来分隔:

string str="aaajbbbscccjdddseee";

   string[] sArray=str.Split(new char[]{ j , s });

foreach(string i in sArray) Response.Write(i.ToString() + "<br>");

输出结果:
aaa
bbb
ccc
ddd
eee


3、用单个字符来分隔:

string str="aaajbbbjccc";

string[] sArray=str.Split( j );

foreach(string i in sArray) Response.Write(i.ToString() + "<br>");

输出结果:
aaa
bbb
ccc

Split重载方法:
        1.public string[] Split(params char[] separator);
        2.public string[] Split(char[] separator, int count);
        3.[ComVisible(false)]
        public string[] Split(char[] separator, StringSplitOptions options);   
        4.[ComVisible(false)]
           public string[] Split(string[] separator, StringSplitOptions options);
        5.[ComVisible(false)]
           public string[] Split(char[] separator, int count, StringSplitOptions options);
        6.[ComVisible(false)]
           public string[] Split(string[] separator, int count, StringSplitOptions options);

        StringSplitOptions options:前后有空项时是否返回空项
        如:/2008/1/1    以‘/’分割
               1)StringSplitOptions.None                                  返回 空 2008 1 1
               2)StringSplitOptions.RemoveEmptyEntries    返回 2008 1 1
        int count:返回分割的项目数