Results 1 to 8 of 8

Thread: Does InStr still work?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Does InStr still work?

    In VB6, we had InStr. Does that still work in VB.Net?

    I want to see if a string, s$, contains "1234".

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Does InStr still work?

    It still works, but there are better options. Look at IndexOf in MSDN, but there are other options, as well. You can use InStr, but you will probably be better off not doing so.
    My usual boring signature: Nothing

  3. #3
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Does InStr still work?

    There is also the Contains method of a string object that returns a boolean.
    Code:
    Dim s As String = "Hello"
    If s.Contains("Hell") Then
        MessageBox.Show("Found It!!!")
    End If
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: Does InStr still work?

    Hi DroopyPawn
    use the myString.Contains("1234"). Like Shaggy Hiker says there are many other methods like myString.Remove Or myString.Replace etc...
    Intellisense is your best friend, use it.
    Hope this helps
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  5. #5
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Does InStr still work?

    Just to throw my two cents in - and nothing against Shaggy, who is a good man - but IMHO, I see nothing wrong with using the "legacy" VB functions. MS made these available in .NET, so why not use them? I have seen a lot of arguments against doing this by ".NET purists", but I respectfully disagree.
    I have found their availability has helped my transition greatly. To use Instr, Mid, Left, Right, etc. just make sure you have an Imports statement at the top of your module:
    Imports Microsoft.VisualBasic
    - or, you can use an alias -
    Imports VB = Microsoft.VisualBasic
    Certain functions (like "Left") will need to be qualified, i.e.:
    FirstInit = VB.Left(FullName, 1)

    On the other hand, the .NET framework does have a lot to offer that was never available in VB6 (the "Contains" example that bmahler pointed out is just one small example). The more you use VB.NET/200X, the more cool things you'll see and make use of, but I for one would not hesitate to use the old-school functions as well. (As I said, there will be those that disagree with that, some strongly.)
    "It's cold gin time again ..."

    Check out my website here.

  6. #6
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Does InStr still work?

    The reason that people say to not use them is that these were merely added to add the ability to upgrade projects from vb6 to .net. There is no guarantee that they will be in all future frameworks so if you continue to use legacy code you can not guarantee that your projects will upgrade in the future. It is best practice to not use those functions wherever possible. If you are upgrading a huge project from vb6 to .net you may not have the time or resources to remove all of those calls, but if you are developing in .net you should not be using legacy code.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Does InStr still work?

    Quote Originally Posted by BruceG
    Just to throw my two cents in - and nothing against Shaggy, who is a good man - but IMHO, I see nothing wrong with using the "legacy" VB functions. MS made these available in .NET, so why not use them? I have seen a lot of arguments against doing this by ".NET purists", but I respectfully disagree.
    In general, I agree with all you have said, and have made similar arguments myself. However, take a look at this thread:

    http://www.vbforums.com/showthread.php?t=508836

    I started off saying "go ahead and use the legacy function", but ended up testing the two different techniques, and found that there is actually a benefit to using the .NET technique over the legacy technique. Of course, in reality, nobody would see a difference as minor as that, but it's good to keep in mind that there actually IS a difference. I haven't tested this case, but I would be surprised if it proved to be much different that the example in the thread, since both are cases of parsing strings.
    My usual boring signature: Nothing

  8. #8
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Does InStr still work?

    If you look at the libraries and what they are doing it is very similar
    The .net String.Contains method calls executes the foloowing code
    Code:
        Public Function Contains(ByVal value As String) As Boolean
            Return (Me.IndexOf(value, StringComparison.Ordinal) >= 0)
        End Function
    
        Public Function IndexOf(ByVal value As String, ByVal comparisonType As StringComparison) As Integer
            Return Me.IndexOf(value, 0, Me.Length, comparisonType)
        End Function
    
        Public Function IndexOf(ByVal value As String, ByVal startIndex As Integer, ByVal count As Integer, ByVal comparisonType As StringComparison) As Integer
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value" & ChrW(65533))
            End If
            If ((startIndex < 0) OrElse (startIndex > Me.Length)) Then
                Throw New ArgumentOutOfRangeException("startIndex" & ChrW(65533), Environment.GetResourceString("ArgumentOutOfRange_Index" & ChrW(65533)))
            End If
            If ((count < 0) OrElse (startIndex > (Me.Length - count))) Then
                Throw New ArgumentOutOfRangeException("count" & ChrW(65533), Environment.GetResourceString("ArgumentOutOfRange_Count" & ChrW(65533)))
            End If
            Select Case comparisonType
                Case StringComparison.CurrentCulture
                    Return CultureInfo.CurrentCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.None)
    
                Case StringComparison.CurrentCultureIgnoreCase
                    Return CultureInfo.CurrentCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.IgnoreCase)
    
                Case StringComparison.InvariantCulture
                    Return CultureInfo.InvariantCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.None)
    
                Case StringComparison.InvariantCultureIgnoreCase
                    Return CultureInfo.InvariantCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.IgnoreCase)
    
                Case StringComparison.Ordinal
                    Return CultureInfo.InvariantCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.Ordinal)
    
                Case StringComparison.OrdinalIgnoreCase
                    Return TextInfo.IndexOfStringOrdinalIgnoreCase(Me, value, startIndex, count)
    
                Case Else
                    Throw New ArgumentException(Environment.GetResourceString("NotSupported_StringComparison" & ChrW(65533)), "comparisonType" & ChrW(65533))
            End Select
        End Function
    Whereas the legacy code calls this code

    Code:
        Public Shared Function InStr(ByVal String1 As String, ByVal String2 As String, <OptionCompare()> Optional ByVal [Compare] As CompareMethod = 0) As Integer
            If ([Compare] = CompareMethod.Binary) Then
                Return (Strings.InternalInStrBinary(0, String1, String2) + 1)
            End If
            Return (Strings.InternalInStrText(0, String1, String2) + 1)
        End Function
    
        Private Shared Function InternalInStrText(ByVal lStartPos As Integer, ByVal sSrc As String, ByVal sFind As String) As Integer
            Dim num2 As Integer
            If (Not sSrc Is Nothing) Then
                num2 = sSrc.Length
            Else
                num2 = 0
            End If
            If ((lStartPos > num2) OrElse (num2 = 0)) Then
                Return -1
            End If
            If ((Not sFind Is Nothing) AndAlso (Not sFind.Length = 0)) Then
                Return Utils.GetCultureInfo.CompareInfo.IndexOf(sSrc, sFind, lStartPos, (CompareOptions.IgnoreWidth Or (CompareOptions.IgnoreKanaType Or CompareOptions.IgnoreCase)))
            End If
            Return lStartPos
        End Function
    Like I stated above the reason that you should not use legacy code is that there is no guarantee as to how long Microsoft will have support for it. Future versions of the framework may not support it anymore at a certain point and for you to upgrade then it will just cause you issues.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width