求正则表达式,字符串在另一字符串出现次数.

xiays 2004-09-10 11:26:54
求一个字符串在另一个字符串中的个数,由于效率原因不希望使用循环来遍历长的字符串,所以求最高效的算法.
...全文
263 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rgwfeng2 2004-09-22
  • 打赏
  • 举报
回复
mark
siugwan 2004-09-10
  • 打赏
  • 举报
回复
gz
jimh 2004-09-10
  • 打赏
  • 举报
回复
saucer(思归/MVP),写得好
saucer 2004-09-10
  • 打赏
  • 举报
回复
sorry, change

i += s1.Length;
==>
i += s.Length;

or if you insist

using System.Text.RegularExpressions;

string s = @"a\w";
string s2 = @"defa\wdfdfsdfa\wddfdsaw";

Regex re = new Regex(Regex.Escape(s), RegexOptions.IgnoreCase|RegexOptions.Singleline);
MatchCollection mc = re.Matches(s2);
Console.WriteLine(mc.Count);

simonllf 2004-09-10
  • 打赏
  • 举报
回复
MARK
saucer 2004-09-10
  • 打赏
  • 举报
回复
you don't need to use regular expressions, and there is no good way with regular expressions, but try

string s = "一个字符串";
string s2 = "另一个字符串";
int n = 0;
s = s.ToLower();
s2 = s2.ToLower();
int i = 0;
while ((i=s2.IndexOf(s, i)) >=0)
{
n++;
i += s1.Length;
}
xiays 2004-09-10
  • 打赏
  • 举报
回复
谢谢,思归大侠
eTopFancy 2004-09-10
  • 打赏
  • 举报
回复
思路的问题,还是思归No 1

110,534

社区成员

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

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

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