How to ignorecase when using string.text.contains?

后端 未结 9 811
情深已故
情深已故 2021-01-07 18:11

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:

 D         


        
相关标签:
9条回答
  • 2021-01-07 19:04

    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

    0 讨论(0)
  • 2021-01-07 19:04

    Or you can use RegularExpressions like this.

    First, import the RegularExpressions:

    Imports System.Text.RegularExpressions
    

    then try this code:

    Dim match As Match = Regex.Match(Textbox1.text,"My house is cold",RegexOptions.IgnoreCase)
    If match.Success Then
       Msgbox(match.Value)
    End If
    
    0 讨论(0)
  • 2021-01-07 19:06

    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
    
    0 讨论(0)
提交回复
热议问题