title: Count number of spaces before index in java
body: <p>I am dealing with long strings, and they will have a certain number of spaces in places in the middle. I know what the index of the beginning letter of the word after the space, how can i count the number of spaces that come before that index in java? Thank you for your time.</p>
  
  <p>This is what I tried to do:</p>
  
  <pre><code>int num = s.indexOf("name");
  char t = ' ';
  int j = 0;
  while(t == ' ') {
  t = s.charAt((num - j));
      j++;
  }
  int spaceCount = num - ((num - j) + 1);
  String space = "";
  for(int i = 0; i &lt; spaceCount; i++) {
      space += " ";
  }
  </code></pre>
  
  <p>Then I would add the space variable to the index "num". It doesn't seem to be working, though. Any suggestions?</p>