user1632018
user1632018

Reputation: 2555

How to ignorecase when using string.text.contains?

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.

Upvotes: 25

Views: 67198

Answers (9)

supwat
supwat

Reputation: 11

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

Upvotes: 0

Adam Pine
Adam Pine

Reputation: 387

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

Upvotes: 5

Sandy
Sandy

Reputation: 31

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   

                                               '

Upvotes: 0

xcrookedxedge
xcrookedxedge

Reputation: 51

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

Upvotes: 2

beppe9000
beppe9000

Reputation: 1114

What about this?

<Runtime.CompilerServices.Extension>
Function InStr(s As String, find As String) As Boolean
    Return s.ToLower.Contains(find.ToLower)
End Function

Upvotes: 0

AI Generated Response
AI Generated Response

Reputation: 8845

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

Upvotes: 15

Muteb A.
Muteb A.

Reputation: 21

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

Upvotes: 1

Elias
Elias

Reputation: 743

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.

Upvotes: 0

Marcel Gosselin
Marcel Gosselin

Reputation: 4716

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.

Upvotes: 46

Related Questions