如果需要在一个字符串中找出一个特定串所有出现的位置,可以采用下面代码:
public
int
[] GetSubStrCountInStr(String str, String substr,
int
StartPos)
... {
int foundPos = -1;
int count = 0;
List<int> foundItems = new List<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());
}
... {
int foundPos = -1;
int count = 0;
List<int> foundItems = new List<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());
}