title: How to use string.Substring(char, char) instead string.Substring(int, int)?
body: <p>I made the extension method that returns a string between <code>leftChar</code> and <code>rightChar</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    public static string Substring(this string This, char leftChar, char rightChar)
          {
              int i_left = This.IndexOf(leftChar);
              if (i_left &gt;= 0)
              {
                  int i_right = This.IndexOf(rightChar, i_left);
                  if (i_right &gt;= i_left)
                  {
                      return This.Substring(i_left + 1, i_right - i_left - 1);
                  }
              }
              return null;
          }
  </code></pre>
  
  <p>When I invoke it up like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    string str = "M(234:342);".Substring('(', ')');
  </code></pre>
  
  <p>I get the exception:</p>
  
  <blockquote>
    <p>System.ArgumentOutOfRangeException: startIndex не может быть больше,
    чем длина строки. Имя параметра: startIndex    в
    System.String.Substring(Int32 startIndex, Int32 length)</p>
  </blockquote>
  
  <p>The namespace of the extension method and the invoking code is correct. IntelliSense suggest me 3 extension methods (two basic and one mine).</p>
  
  <blockquote>
    <p>(extension) string string.Substring(char leftChar, char rightChar)</p>
  </blockquote>
  
  <p>It is clear that <code>string.Substring(int startIndex, int Length)</code> overlaps with my extension method because the compiler casts char to int automaticaly.</p>
  
  <p><strong>Is there a way to force the compiler not to do the cast <code>char</code> to <code>int</code>?</strong></p>
  
  <hr>
  
  <ul>
  <li>If I rename the extension method (like <code>Substring2</code>) all works.</li>
  <li>If I change <code>chars</code> to <code>params char[]</code>, it does not work, if I invoke it like params, but of course works like array.</li>
  <li>If I delete the extension method, <code>"M(234:342);".Substring('(', ')')</code> does not throw any compilation error, but still throw the exception.</li>
  <li>If I change <code>chars</code> to <code>strings</code> in the extension method it also works. This is my current favorite solution for this issue - to combine my <code>Substring(char, char)</code> with <code>Substring(string, string)</code> with checking lengths of input strings.</li>
  </ul>