title: .NET equivalent of the old vb left(string, length) function
body: <p>As a non-.NET programmer I'm looking for the .NET equivalent of the old Visual Basic function <code>left(string, length)</code>. It was lazy in that it worked for any length string. As expected, <code>left(&quot;foobar&quot;, 3) = &quot;foo&quot;</code> while, most helpfully, <code>left(&quot;f&quot;, 3) = &quot;f&quot;</code>.</p>
  <p>In .NET <code>string.Substring(index, length)</code> throws exceptions for everything out of range. In Java I always had the Apache-Commons lang.StringUtils handy. In Google I don't get very far searching for string functions.</p>
  <hr/>
  <p><strong>@Noldorin</strong> - Wow, thank you for your VB.NET extensions! My first encounter, although it took me several seconds to do the same in C#:</p>
  <pre><code>public static class Utils
  {
      public static string Left(this string str, int length)
      {
          return str.Substring(0, Math.Min(length, str.Length));
      }
  }
  </code></pre>
  <p>Note the static class and method as well as the <code>this</code> keyword. Yes, they are as simple to invoke as <code>&quot;foobar&quot;.Left(3)</code>. See also <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" rel="noreferrer">C# extensions on MSDN</a>.</p>