六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 34|回复: 0

C# 找出一个子串在另一字符串中出现的所有位置

[复制链接]

升级  92.5%

932

主题

932

主题

932

主题

探花

Rank: 6Rank: 6

积分
2850
 楼主| 发表于 2013-2-1 09:51:40 | 显示全部楼层 |阅读模式
如果需要在一个字符串中找出一个特定串所有出现的位置,可以采用下面代码:
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">publicint[]GetSubStrCountInStr(Stringstr,Stringsubstr,intStartPos)
...{
intfoundPos=-1;
intcount=0;
List
<int>foundItems=newList<int>();

do
...{
foundPos
=str.IndexOf(substr,StartPos);
if(foundPos>-1)
...{
StartPos
=foundPos+1;
count
++;
foundItems.Add(foundPos);
}

}
while(foundPos>-1&&StartPos<str.Length);

return((int[])foundItems.ToArray());
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表