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

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

+4
source share
3 answers

An example can explain a lot. This is the Mid source code 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 bit more complicated for Instragains IndexOf, because you can use the comparison parameter, but in this case the internal code used in the Microsoft library. VisualBasic COMPATIBILITY (Bold is my) falls again inside the basic methods provided .NET Framework

, , VB6, . , , NET Framework.

+8

, Mid() Instr(), , . , (, Xbox Windows Phone) . , , . , .Net, , , .

, , , Trim() , . :

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

, .Net-.

+5

, System.String, , . #, VB, , System.String. System.String, AFAIK Microsoft.VisualBasic, EndsWith, . VB VB6 .., .

- , VB . , , , Mid "" , Substring. . ; . , VB System.String . , VB5, VB.

+2

All Articles