如果需要在一个字符串中找出一个特定串所有出现的位置,可以采用下面代码:
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());
}
本文介绍了一个用于在字符串中查找特定子串所有出现位置的方法。通过一个公共方法 GetSubStrCountInStr 实现了这一功能,该方法返回一个整数数组,包含了子串在主字符串中的所有起始位置。
2407

被折叠的 条评论
为什么被折叠?



