As a non.net programmer, I am looking for the .net equivalent of the old vb left(string, length) function. This was lazy as it worked for any string length. As expected, left("foobar", 3) = "foo" while, most useful, left("f", 3) = "f" .
In .net string.Substring(index, length) throws exceptions for everything out of range. In Java, I always had Apache-Commons lang.StringUtils. At Google, I am not very good at string functions.
Edit:
@Noldorin - Wow, thanks for your vb.net extensions! My first meeting, although it took me a few seconds to do the same in C #:
public static class Utils { public static string Left(this string str, int length) { return str.Substring(0, Math.Min(length, str.Length)); } }
Note the static class and method, as well as the this . Yes, they are as easy to call as "foobar".Left(3) . See Also C # Extensions in msdn .
Josh May 09 '09 at 9:01 p.m. 2009-05-09 21:01
source share