26

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.

9 Answers 9

46

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
15

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 

2 Comments

I like this solution better.
This causes issues w/ globalization specifically w/ Turkish i. I'd be careful w/ this method.
6

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

2 Comments

This causes issues w/ globalization specifically w/ Turkish i. I'd be careful w/ this method.
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.
3

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

2 Comments

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.
Danke für das +1! I mean: Thank you for the +1
1

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 

1 Comment

this looks like an overkill
0

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.

Comments

0

What about this?

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

Comments

0

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 ' 

1 Comment

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?
0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.