title: C# substring out of range
body: <p>The problem I am having is that I need to be able to loop over a string, returning 5 characters after the position of the index, then starting 5 characters after.</p>
  
  <p>However when there are less than 5 characters left an out of range error occurs, I thought it would just print the remaining characters of the string.</p>
  
  <pre><code>string string1 = "ghuitghtruighr";
  for (int index = 0; index &lt; string1.Length; index += 5)
  {
      string subString = string1.Substring(i, 5);
      Console.WriteLine(subString);
  }
  </code></pre>
  
  <p>How can I get it to print what's left of the string when whats remaining is less than 5?</p>