我有一个大字符串需要解析,我需要找到所有extract"(me,i-have lots. of]punctuation
的实例,并将每个实例的索引存储到一个列表中。
假设这个字符串片段在较大的字符串的开头和中间出现,两个实例都会被找到,并将它们的索引添加到List
中。该List
将包含0
和另一个索引,无论它是什么。
我已经尝试过使用string.IndexOf
方法,它几乎可以完成我想要的功能,但我编写的代码并不起作用,而且我一直无法确定问题所在:
List<int> inst = new List<int>();
int index = 0;
while (index < source.LastIndexOf("extract\"(me,i-have lots. of]punctuation", 0) + 39)
{
int src = source.IndexOf("extract\"(me,i-have lots. of]punctuation", index);
inst.Add(src);
index = src + 40;
}
inst
= 列表source
= 大字符串
还有更好的想法吗?