Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  Asp.net  >  正文 asp.net字符串分割函数用法

asp.net字符串分割函数用法

发布时间:2018-09-08   编辑:www.jquerycn.cn
jquery中文网为您提供asp.net字符串分割函数用法等资源,欢迎您收藏本站,我们将为您提供最新的asp.net字符串分割函数用法资源
本文章介绍了asp中字符分割的多种方法,有需要的朋友可以参考一一下哈,好了费话不说多了看看吧.

先来看个简单的实例

但是其数组长度却是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()方法下输入下面的程序。

<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('copy2516')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy2516" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

string s="abcdeabcdeabcde";
string[] sArray=s.Split('c');
foreach(string i in sArray)
Console.WriteLine(i.ToString());

输出下面的结果:
ab
deab
deab
de

</td> </tr> </tbody> </table>

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

</td> </tr> </tbody> </table>

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());

</td> </tr> </tbody> </table>

输出下面的结果:
agc
mac
ggg
ytx


还有一种比较不常用的方法


代码如下:

<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('copy1569')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy1569" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">string str = "reterry[111cn.net]是脚本之家[111cn.net]的站长";
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() 函数

[关闭]