为什么我的变量nextSpaceIterator不会更新到nextSpace之后的空间索引?我能够使用IndexOf查找空间,为什么我无法找到下一个空间?
int firstSpace = 0;
int nextSpace = 0;
int nextSpaceIterator = 0;
nextSpace = someInputString.IndexOf((char)ConsoleKey.Spacebar);
//find next space
Console.WriteLine(someInputString.Substring(firstSpace, nextSpace - firstSpace));
// Print word between spaces
firstSpace = nextSpace;
// Starting point for next step is ending point of previous step
nextSpaceIterator = someInputString.IndexOf((char)ConsoleKey.Spacebar, nextSpace);
// Find the next space following the previous one, then repeat.
最初我使用了for循环,但我已经将代码分解成单个语句以尝试找到问题,但我不能。 一切工作到这一点。不应该
nextSpaceIterator = someInputString.IndexOf((char)ConsoleKey.Spacebar, nextSpace);
回报比nextSpace不同的价值?
我想你必须在'nextspace + 1'开始第二次搜索。但不要忘记检查这个新的起始值是否仍在字符串的长度内。 – gdir
这个问题提供了与你的问题相同的答案https://stackoverflow.com/a/4578768/3645638 – Svek
可能的重复[在C#中查找大字符串中的子字符串的所有位置](https://stackoverflow.com /问题/ 2641326 /发现-所有位对的一子串-IN-A-大字符串在-C-尖锐) – Fabio