tags:

views:

1768

answers:

5

As a non .net programmer I'm looking for the .net equivalent of the old vb function left(string, length). It was lazy in that it worked for any length string. As expected, left("foobar", 3) = "foo" while, most helpfully, left("f", 3) = "f".

In .net string.Substring(index, length) 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.

Edit:

@Noldorin - Wow, thank you for your vb.net extensions! My first encounter, although it took me several 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 keyword. Yes, they are as simple to invoke as "foobar".Left(3). See also c# extensions on msdn.

A: 

You can either wrap the call to substring in a new function that tests the length of it as suggested in other answers (the right way) or use the Microsoft.VisualBasic namespace and use left directly (generally considered the wrong way!)

RobS
You should not use Try...Catch to catch something that you can very easily check for beforehand.
Guffa
Yup, you're right and I edited it exactly the same time as your comment...
RobS
Why is using the VisualBasic namespace considered the "wrong way"? That's precisely why it is included. It's part of VB.
Chris Dunaway
+2  A: 

you could make your own

private string left(string inString, int inInt)
{
    if (inInt > inString.Length)
        inInt = inString.Length;
    return inString.Substring(0, inInt);
}

edit: oh sorry mines in C#, you may have to change it for vb

Jean-Bernard Pellerin
+7  A: 

Here's an extension method that will do the job.

<System.Runtime.CompilerServices.Extension()> _
Public Function Left(ByVal str As String, ByVal length As Integer)
    Return str.Substring(0, Math.Min(str.Length, length))
End Function

This means you can use it just like the old VB Left function (i.e. Left("foobar", 3) ) or using the newer VB.NET syntax, i.e.

Dim foo = "f".Left(3) ' foo = "f"
Dim bar = "bar123".Left(3) ' bar = "bar"
Noldorin
Was just typing up an example of an extension method.
j0tt
As far as I know, extension methods don't work for VB.NET
Oded
@Oded: They most certainly do in VB.NET 9. You need to specify the Extension attribute, which is quite different to the method for C#. See http://msdn.microsoft.com/en-us/library/bb384936.aspx
Noldorin
The original question stated nothing about an extension method. Seems to be going off on a tangent.
Andrew Robinson
@Andrew Robinson: Not really, I think. Just because he didn't mention it, it doesn't mean it's not a valid (addition to the) answer. In fact, because he didn't refer to it, I suspected it was likely to be of *more* interest to him. Adding extra info that could be helpful never hurts. :)
Noldorin
+3  A: 

Another one line option would be something like the following:

myString.Substring(0, Math.Min(length, myString.Length))

Where myString is the string you are trying to work with.

Oded
+1  A: 

Add a reference to the Microsoft.VisualBasic library and you can use the Strings.Left which is exactly the same method.

Garry Shutler