0

Why won't my variable nextSpaceIterator update to the index of the space following 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. 

Originally I used a for loop but I've broken down the code into individual statements to try to find the problem and I can't. Everything works up until this point. Shouldn't

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

return a different value than nextSpace?

  • 2
    I think you have to start the second search at `nextspace + 1`. But don't forget to check that this new start value is still inside the length of the string. – gdir Aug 06 '17 at 02:17
  • This other question provides the same answer to your problem https://stackoverflow.com/a/4578768/3645638 – Svek Aug 06 '17 at 02:50
  • Possible duplicate of [Finding ALL positions of a substring in a large string in C#](https://stackoverflow.com/questions/2641326/finding-all-positions-of-a-substring-in-a-large-string-in-c-sharp) – Fabio Aug 06 '17 at 05:00

2 Answers2

3

Based on comments in your code (Print word between spaces), you want get string between spaces

Console.WriteLine(someInputString.Substring(firstSpace, nextSpace - firstSpace));`   
// Print word between spaces

If so, then use String.Split Method

var words = someInputString.Split((char)ConsoleKey.Spacebar);

var firstWord = words[0];
var secondWord = words[1]; // If you sure that there at least two words

// or loop the result
foreach (var word in words)
{
    Console.WriteLine(word);
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
2
nextSpace = someInputString.IndexOf((char)ConsoleKey.Spacebar);
nextSpaceIterator = someInputString.IndexOf((char)ConsoleKey.Spacebar, nextSpace);

The nextSpaceIterator will return the same position as nextSpace, since the offset you provided begins at the same index of nextSpace.

For example:

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

Solution. You need to change the offset to advance forward:

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

Gotchas. You will get an index out of bounds exception, if the final character in the string is a space. So you should always check for that, which can be simply checking against the total length or count of the string -- oh, and don't forget that indexes start at zero.

Svek
  • 12,350
  • 6
  • 38
  • 69