xpda
xpda

Reputation: 15813

Should mid and instr be used, or indexof and substring?

Some VB string functions have similar methods in System.String, such as mid and substring, instr and indexof. Is there a good reason to use one or the other?

Upvotes: 5

Views: 5138

Answers (3)

NoAlias
NoAlias

Reputation: 9193

I'd love to say you should use Mid() or Instr() because some of us have been using those functions for years, but I'd recommend against using those throwbacks. Mostly because the portable target platforms (like for Xbox and Windows Phone) do not support them. To me that's a sign that they're going to deprecate sooner than later. I've also read the .Net versions seem to perform better, but can't find any statistics to support that claim right now.

One other interesting note that is somewhat related is that the way in which the Trim() function deals with break lines is different. Sample code:

Dim strTest As String = ControlChars.NewLine ' OR Environment.NewLine OR vbNewLine
Dim oldLength As Integer = Len(Trim(strTest)) '2
Dim newLength As Integer = strTest.Trim().Length '0

So be careful if you're porting code to the .Net versions.

Upvotes: 5

Steve
Steve

Reputation: 216293

An example could explain a lot. This is the source code of Mid from Microsoft.VisualBasic

public static string Mid(string str, int Start, int Length)
{
    if (Start <= 0)
    {
        throw new ArgumentException(Utils.GetResourceString("Argument_GTZero1", new string[] { "Start" }));
    }
    if (Length < 0)
    {
        throw new ArgumentException(Utils.GetResourceString("Argument_GEZero1", new string[] { "Length" }));
    }
    if ((Length == 0) || (str == null))
    {
        return "";
    }
    int length = str.Length;
    if (Start > length)
    {
        return "";
    }
    if ((Start + Length) > length)
    {
        return str.Substring(Start - 1);
    }
    return str.Substring(Start - 1, Length);
}

At the end of the day they call Substring....
The story is a little more complex for Instr agains IndexOf because you could use a compare parameter but also in that case the internal code used in the Microsoft.VisualBasic COMPATIBILITY (Bold is mine) library falls again inside the base methods provided by the NET Framework.

Of course, if you need only to maintain an old program ported from the VB6 days, then it is absolutely correct to use these methods. Instead if you plan to continue the evolution of your program or you build a new one I suggest to switch to the NET Framework core methods as soon as possible.

Upvotes: 11

Stuart Whitehouse
Stuart Whitehouse

Reputation: 1441

The reason I usually prefer to use the System.String one is that it is more compatible with other languages. If using c# as well as VB it's much less confusing to stick to the System.String ones. There are also a number of System.String functions which don't AFAIK have equivalents in Microsoft.VisualBasic, such as EndsWith and it's a bit odd to use a mixture. The VB ones are for compatibility with VB6 etc which is ancient now.

However - I do sometimes like the fact that the VB versions are more fault tolerant. The Mid example that Steve posted shows that Mid returns "" in cases which would have thrown an exception with Substring. There are similar differences with some of the others. I have found that quite useful in the past; you can end up writing those checks yourself before calling Substring. It also means that editing code using the old VB style commands to System.String can introduce some unexpected exception. I work on one project which started in VB5, and I learnt not to replace the old versions VB without a reason quite quickly.

Upvotes: 2

Related Questions