How to ignorecase when using string.text.contains?

后端 未结 9 810
情深已故
情深已故 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 18:47

    What about this?

    <Runtime.CompilerServices.Extension>
    Function InStr(s As String, find As String) As Boolean
        Return s.ToLower.Contains(find.ToLower)
    End Function
    
    0 讨论(0)
  • 2021-01-07 18:50

    I use below code to search/confirm if string in TEXTBOX1 was in "c:\testsearch.txt".

        Imports System.IO
    
    Private Function sinf(path As String, match As String) As Boolean 
        Dim s As String = File.ReadAllText(path).ToLower
        Return s.Contains(match.ToLower)
    End Function
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim st As String
        st = TextBox1.Text
        If sinf("c:\testsearch.txt", st) = True Then 
            MsgBox("Found if")
        Else
            MsgBox("Try again")
        End If
    End Sub
    
    0 讨论(0)
  • 2021-01-07 18:51

    use the InStr example. "contains" fails if ether compare is nothing.

    'if we found something...

    If InStr(1, value, search, vbTextCompare) > 0 Then
    
    Beep
    
    End If   
    
                                                   '
    
    0 讨论(0)
  • 2021-01-07 18:52

    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.

    0 讨论(0)
  • 2021-01-07 18:59

    this is how I solved my problem of making String.Contains become case insensitive.

    Dim s as string = "My HoUsE iS cOlD".ToUpper
    
    If s.Contains("MY HOUSE IS COLD") Then Exit Sub
    

    For my particular issue, the string that I was checking was housed within a TextBox.

    I hope this helps.

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

    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

    0 讨论(0)
提交回复
热议问题