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
What about this?
<Runtime.CompilerServices.Extension>
Function InStr(s As String, find As String) As Boolean
Return s.ToLower.Contains(find.ToLower)
End Function
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
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
'
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.
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.
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