C# 我可以使用IndexOf来查找空间,为什么可以';我不能也这样找到下一个空间吗?

C# 我可以使用IndexOf来查找空间,为什么可以';我不能也这样找到下一个空间吗?,c#,indexof,C#,Indexof,为什么我的变量nextSpaceIterator不更新到nextSpace后面的空间的索引 int firstSpace = 0; int nextSpace = 0; int nextSpaceIterator = 0; nextSpace = someInputString.IndexOf((char)ConsoleKey.Spacebar); //find next space Console.WriteLine(someInputString.Sub

为什么我的变量nextSpaceIterator不更新到nextSpace后面的空间的索引

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不同的值

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. 
nextSpaceIterator
将返回与
nextSpace
相同的位置,因为您提供的偏移量开始于
nextSpace
相同的索引

例如:

string foo = "The quick brown fox";

//   0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  1  8
//  [T][h][e][ ][q][u][i][c][k][ ][b][r][o][w][n][ ][f][o][x]
//            *                 *                 *    

// in this example the indexes of spaces are at 3, 9 and 15.

char characterToMatch = (char)ConsoleKey.Spacebar;

int first = foo.IndexOf(characterToMatch); // 3

int invalid = foo.IndexOf(characterToMatch, first); // this will still be 3

int second = foo.IndexOf(characterToMatch, first + 1); // 9
int third = foo.IndexOf(characterToMatch, second + 1); // 15
解决方案。您需要更改偏移以向前推进:

nextSpaceIterator = someInputString.IndexOf((char)ConsoleKey.Spacebar, nextSpace+1);
Gotchas.如果
字符串中的最后一个字符是空格,则会出现索引越界异常。因此,您应该始终检查它,这可以简单地检查字符串的总长度或计数——哦,不要忘记索引从零开始

nextSpaceIterator
将返回与
nextSpace
相同的位置,因为您提供的偏移量开始于
nextSpace
相同的索引

例如:

string foo = "The quick brown fox";

//   0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  1  8
//  [T][h][e][ ][q][u][i][c][k][ ][b][r][o][w][n][ ][f][o][x]
//            *                 *                 *    

// in this example the indexes of spaces are at 3, 9 and 15.

char characterToMatch = (char)ConsoleKey.Spacebar;

int first = foo.IndexOf(characterToMatch); // 3

int invalid = foo.IndexOf(characterToMatch, first); // this will still be 3

int second = foo.IndexOf(characterToMatch, first + 1); // 9
int third = foo.IndexOf(characterToMatch, second + 1); // 15
解决方案。您需要更改偏移以向前推进:

nextSpaceIterator = someInputString.IndexOf((char)ConsoleKey.Spacebar, nextSpace+1);

Gotchas.如果
字符串中的最后一个字符是空格,则会出现索引越界异常。因此,您应该始终检查它,这可以简单地检查字符串的总长度或计数——哦,不要忘记索引从零开始。

根据代码中的注释(在空格之间打印单词),您希望在空格之间获得字符串

如果是,则使用


根据代码中的注释(在空格之间打印单词),您需要在空格之间获取字符串

如果是,则使用


我想您必须在
nextspace+1
开始第二次搜索。但别忘了检查这个新的开始值是否仍在字符串长度内。这个问题为您的问题提供了相同的答案。我认为您必须在
nextspace+1
开始第二次搜索。但别忘了检查这个新的起始值是否仍在字符串长度内。这个问题为您的问题提供了相同的答案,可能是重复的