当前位置: 编程技术>.net/c#/asp.net
asp.net Split分割字符串的3种用法
来源: 互联网 发布时间:2014-08-30
本文导语: 需求: 可以把12 33 456 12342 拼起来就是一个字符,因为很多时候我们觉得处理只有几个组的时候用数组很麻烦。 因此,用“|”或者“,”等把他们拼起来在需要时用Split打散即可。 下面介绍下asp.net中Split方法的三种用法,供...
需求:
可以把12 33 456 12342 拼起来就是一个字符,因为很多时候我们觉得处理只有几个组的时候用数组很麻烦。
因此,用“|”或者“,”等把他们拼起来在需要时用Split打散即可。
下面介绍下asp.net中Split方法的三种用法,供大家参考。
方法一:
代码示例:
static void Main(string[] args)
{
string aa = "1234,234523,4324,324";
string[] cc = aa.Split(new char[] { ',' });
foreach (string bb in cc)
{
Console.Write(bb + "/r/n ");
}
}
{
string aa = "1234,234523,4324,324";
string[] cc = aa.Split(new char[] { ',' });
foreach (string bb in cc)
{
Console.Write(bb + "/r/n ");
}
}
方法二:
代码示例:
string aa = "1234,234523,4324,324";
string[] str = aa.Split(',');
foreach (string bb in str)
{
Console.Write(bb + " /r/n");
}
string[] str = aa.Split(',');
foreach (string bb in str)
{
Console.Write(bb + " /r/n");
}
方法三:
代码示例:
static void Main(string[] args)
{
string a = "1,2,3,4,5,6,7,8,9";
string b = ",";
string[] c = Split(a, b);
foreach (string bb in c)
{
Console.Write(bb + " /r/n");
}
}
public static string[] Split(string input, string pattern)
{
string[] arr = System.Text.RegularExpressions.Regex.Split(input, pattern);
return arr;
}
{
string a = "1,2,3,4,5,6,7,8,9";
string b = ",";
string[] c = Split(a, b);
foreach (string bb in c)
{
Console.Write(bb + " /r/n");
}
}
public static string[] Split(string input, string pattern)
{
string[] arr = System.Text.RegularExpressions.Regex.Split(input, pattern);
return arr;
}
调用示例,把文章按空格打散:
代码示例:
public static void Main ()
{
string a="While laughter is is very aerobic activity engages every single organ system";
string b=" ";
string []c=TestDoWhile.Split(a,b);
foreach(string bb in c)
{
Console.Write(bb+" /r/n");
}
}
public static string[] Split(string input,string pattern)
{
string[] arr = System.Text.RegularExpressions.Regex.Split(input,pattern);
return arr;
}
{
string a="While laughter is is very aerobic activity engages every single organ system";
string b=" ";
string []c=TestDoWhile.Split(a,b);
foreach(string bb in c)
{
Console.Write(bb+" /r/n");
}
}
public static string[] Split(string input,string pattern)
{
string[] arr = System.Text.RegularExpressions.Regex.Split(input,pattern);
return arr;
}