asp.net字符串分割函数用法
先来看个简单的实例
但是其数组长度却是25,而不是3。下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split
例如下面我要根据[111cn.net]分割的话
<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy9920')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy9920" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">string[] arrstr2 = str.Replace("[111cn.net]", "$").Split('$');
</td> </tr> </tbody> </table>好了看其它方法
、最简单最常用的方法,以一个指定的字符进行的分割 打开vs.net新建一个控制台项目。然后在Main()方法下输入下面的程序。
string s="abcdeabcdeabcde";
string[] sArray=s.Split('c');
foreach(string i in sArray)
Console.WriteLine(i.ToString());
输出下面的结果:
ab
deab
deab
de
2、使用多个字符进行分割
<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy6671')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy6671" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">string s="abcdeabcdeabcde"
string[] sArray1=s.Split(new char[3]{'c','d','e'});
foreach(string i in sArray1)
Console.WriteLine(i.ToString());
输出下面的结果:
ab
ab
ab
3、使用正则表达式
添加引用
<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy2393')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy2393" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">using System.Text.RegularExpressions;
string content="agcsmallmacsmallgggsmallytx";
string[]resultString=Regex.Split(content,"small",RegexOptions.IgnoreCase)
foreach(string i in resultString)
Console.WriteLine(i.ToString());
输出下面的结果:
agc
mac
ggg
ytx
还有一种比较不常用的方法
代码如下:
string[] arrstr = str.Split(new char[] { '[', 's', 'o', 's', 'u', 'o', '8', '.', 'c', 'o', 'm', ']' });
for (int i = 0; i < arrstr.Length; i )
{
Response.Write(arrstr[i]);
}</td> </tr> </tbody> </table>
您可能感兴趣的文章:
php分割数组示例
php字符串分割函数explode实例
js split函数分割字符串为数组
js split函数的用法说明
PHP extract(数组拆分)作用分析
php分割中文字符串为数组的简单例子
js中Split分割字符串允许存在分割符的例子
php中chunk_split()函数的用法介绍
php 字符串转数组
PHP chunk_split() 函数