Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vb.net

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?

like image 767
xpda Avatar asked Nov 29 '13 17:11

xpda


People also ask

What is Substr and InStr in SQL?

The substr functions allows you to extract a substring from a string. The instr function returns the location of a substring in a string.

What is the string function InStr used for explain with an example?

InStr function finds the position of a specified substring within the string and returns the first position of its occurrence. For example, if you want to find the position of 'x' in 'Excel', using the Excel VBA InStr function would return 2.

How do I use InStr in Crystal Reports?

instr = The instr function returns the position of the first occurrence of one string within another. This position is based on a 1 based index of the characters in the string. *For additional information and examples on formula functions, please go to Help, Crystal Reports Help from within the Crystal Reports program.


3 Answers

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.

like image 137
Steve Avatar answered Nov 12 '22 22:11

Steve


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.

like image 42
NoAlias Avatar answered Nov 12 '22 23:11

NoAlias


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.

like image 22
Stuart Whitehouse Avatar answered Nov 12 '22 23:11

Stuart Whitehouse