谁有搜索字符的通用方法。

hjwzr 2006-12-07 06:24:27
好比有个字符串,里面包含一堆字符串。其中里面有像<a href="test.htm">test</a>这样的字符串,我想要的是这个方法能提供2个入口参数,1个就是字符串的开始特征字符,第2个就是字符串的结束特征字符。当提供这两个入口参数后,就能将开始和结束中间的字符串返回过来。像上面的一样,我提供<a href="test.htm"> 开始特征字符串,我再提供</a>就能将test返回。
谁有这样的方法,给小弟一份啊,多谢。
...全文
137 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SmallMummy 2006-12-08
  • 打赏
  • 举报
回复
用string类中的方法也可以,Indexof() and Subsring,Lastindexof()......
我觉得你使用正则表达式会简单一点
正则为(?<=<a href="test.htm">).*?(?=</a>)
这样的话,结果就为中间的部分test,在(?<=.....)中间写上你的开始特征字符串,(?=..)写上结束部分,注意反义字符

在代码中使用System.Text.RegularExpressions.Regex类
具体lz可以看看msdn
RexZheng 2006-12-08
  • 打赏
  • 举报
回复
测试:

foreach (string text in GetMatchText("1x25642z34156z8dg4xsg36z54235x8tdf56z482374", "x", "z"))
{
Console.WriteLine(text);
}

输出:
25642
sg36
8tdf56
RexZheng 2006-12-08
  • 打赏
  • 举报
回复
using System.Text.RegularExpressions;


static string[] GetMatchText(string input,string beginTag, string endTag)
{
MatchCollection mc = Regex.Matches(input, beginTag + "(?<Text>.*?)" + endTag);
string[] result = new string[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
result[i] = mc[i].Groups["Text"].Value;
}
return result;
}
RexZheng 2006-12-08
  • 打赏
  • 举报
回复
如果说要通用,那就是正则了
Ivan520270 2006-12-07
  • 打赏
  • 举报
回复
用Indexof() and Subsring()。

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧