title: C# - Finding All Indices of a Substring
body: <p><strong>EDIT:</strong> So it turns out what I had before was correct, I was simply counting the indexes wrong. Thank you for your input though.</p>
  
  <p>Working on a method that finds all substring indices in a given string from the user. I am having issues with getting correct positions from userString.IndexOf. I know it's finding all occurrences of the substring, but the integer index is off by a substantial amount.</p>
  
  <pre><code>private static void getSubStringPositions(string userString, string userSubString)
  {
      string subStringPositions = "";
      int position = 0;
  
      if (userString.IndexOf(userSubString, position) == -1)
      {
          Console.WriteLine("Your sub-string was not found in your string.\n");
          return;
      }
      while (position &lt; userString.Length)
      {
          position += userString.IndexOf(userSubString, position);
          subStringPositions += Convert.ToString(position) + ", ";
      }
  
      Console.WriteLine("The occurernce(s) for your sub-string are: " + subStringPositions + "\n\n");
      return;
  }
  </code></pre>
  
  <p>I think it may be an issue with <code>position += userString.IndexOf(userSubString, position);</code> but am not entirely sure how I would go about setting the new start position while maintaing an accurate record of the substring locations.</p>