How to ignorecase when using string.text.contains?

58,060

Solution 1

According to Microsoft you can do case-insensitive searches in strings with IndexOf instead of Contains. So when the result of the IndexOf method returns a value greater than -1, it means the second string is a substring of the first one.

Dim myhousestring As String = "My house is cold"
If txt.Text.IndexOf(myhousestring, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
    Messagebox.Show("Found it")
End If

You can also use other case-insensitive variants of StringComparison.

Solution 2

I'm not a vb.net programmer, but according to Microsoft, you can get the lowercase/uppercase value of the text using the string methods ToUpper() or ToLower(). You can then compare that with "my house is cold" or "MY HOUSE IS COLD".

Dim myhousestring As String = "MY HOUSE IS COLD"
If txt.Text.ToUpper.Contains(myhousestring) Then
    Messagebox.Show("Found it")
End If

Solution 3

Personally I just used:

item.Text.ToLower().Contains("my house is cold")

you could just as well use ToUpper as well.

Caveat: If you are comparing Turkish, or other languages, the ToLower() and ToUpper() also take an option parameter, for "CultureInfo" allowing you to ensure that different languages are handled correctly. You can use an above solution, or you can follow the steps from Microsoft's ToLower Documentation, to add in CultureInfo, to get ToLower context on which language you are about to try to manipulate.

ToLower() with CultureInfo documentation

ToUpper() with CultureInfo documentation

Solution 4

I solved this problem with .toUpper

For example:

Dim UGroup as String = dr.Item(2).ToString().ToUpper
Dim s as String = ds.Item(1).ToString.ToUpper

If s.Contains(UGroup) then MsgBox("Well done!")
Else 
End Sub

Same procedure with .toLower

Share:
58,060

Related videos on Youtube

user1632018
Author by

user1632018

Updated on July 09, 2022

Comments

  • user1632018
    user1632018 almost 2 years

    I am trying to figure out how to check if a string contains another while ignoring case using .text.contains.

    As it stands right now If I do this:

     Dim myhousestring As String = "My house is cold"
        If txt.Text.Contains(myhousestring) Then
        Messagebox.Show("Found it")
        End If
    

    It will only return a match if it is the exact same case. So if the user typed "my house is cold", it would not be a match.

    How can I do this? If it is not possible I could probably just use regex instead with ignorecase. Any help would be appreciated.

  • user1632018
    user1632018 over 11 years
    ahh, this is exactly what I was looking for. I knew their was was another string method that could be used to compare strings. Thanks alot. I appreciate it.
  • user4951
    user4951 almost 8 years
    I like this solution better.
  • beppe9000
    beppe9000 almost 8 years
    this looks like an overkill
  • jt000
    jt000 over 7 years
    This causes issues w/ globalization specifically w/ Turkish i. I'd be careful w/ this method.
  • jt000
    jt000 over 7 years
    This causes issues w/ globalization specifically w/ Turkish i. I'd be careful w/ this method.
  • xcrookedxedge
    xcrookedxedge about 7 years
    Sorry it's a german word. Commata means Commas. So "," this is only importan in my code. But i deleted it to not spread confusion.
  • xcrookedxedge
    xcrookedxedge about 7 years
    Danke für das +1! I mean: Thank you for the +1
  • Tom
    Tom almost 7 years
    InStr's first parameter isn't needed in this case, and you might want to match up your input parameters with the questions. I also don't think this is case-insensative according to MSDN?
  • Adam Pine
    Adam Pine almost 7 years
    The question doesn't seem to mention that it needs to support Turkish... especially when we are comparing the text, with English... The Culture indifferent compare isn't going to translate from Turkish to English, and THEN compare, it's just going to compare the characters.