Check if a String contains a specified String in VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 5.00 out of 5)
 

This code will search a string to check if it contains a specific string. Works for VB.NET, VB 2008, Visual Basic 2010, and Newer.

This code Is case sensitive.

        '
        'Check if a String contains a specified string. This code IS Case Sensitive.
        'Check the second set of codes below for Non-Case Sensitive.
        '
        Dim str As String = "This string has some text"

        If str.Contains("string") Then

            MessageBox.Show("Yeps, that string is in here!")

        Else

            MessageBox.Show("No, the string is not in here!")

        End If

 

The code here is Not case sensitive.

        '
        'This code is NOT case sensitive.
        '
        Dim str As String = "This STring has some text"

        If LCase(str).Contains(LCase("striNg")) Then

            MessageBox.Show("Yeps, that string is in here!")

        Else

            MessageBox.Show("No, the string is not in here!")

        End If

Minor Revision: 2015

13 thoughts on “Check if a String contains a specified String in VB.NET

  1. Ryan
    If InStr(1, strString, "example") Then
        'string contains the substring "example"
    Else
        'string does not contain the substring "example"
    End If
    
    Reply
    1. SkyRRaN

      Thanks for your comment, it was a great solution for me to replace the “String.Contain()” function that it doesn’t work in my vb.net project.

      Thank you so much ! SkyRRaN

      Reply
  2. Ste
            'Check if a String contains a specified string.
    
            Dim str As String = "This string has some text"
    
            textbox1.text = str
    
            If textbox1.text.contains("string") Then
    
                MessageBox.Show("Yeps, that string is in here!")
    
            Else
    
                MessageBox.Show("No, the string is not in here!")
    
            End If
    
    Reply
  3. Jen

    It does not make it clear that if you search for “w” in a string that says “Hello World”, it will not find it. In other words, it is NOT case-sensitive… this can cause confusion for newbies!

    Reply
  4. Jason Post author

    Hi Jen, I tested this code and it IS case sensitive. If you don’t want it to be case sensitive you can modify the code alittle like below:

            Dim str As String = "This STring has some text"
    
            If LCase(str).Contains(LCase("striNg")) Then
    
                  MessageBox.Show("Yeps, that string is in here!")
    
            Else
    
                  MessageBox.Show("No, the string is not in here!")
    
            End If
    
    Reply
  5. wade

    lol at “Frustrated”. Your comment was posted this year in 2011, and your stating your programming in VB6! Get with the times, wow, VB6 is stone-age programming. Upgrade to .net 2010 or at least 2008. You gotta be kidding!

    Reply

Leave a Reply

Your email address will not be published.