Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignorecase when using string.text.contains?

Tags:

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.

like image 891
user1632018 Avatar asked Dec 28 '12 03:12

user1632018


People also ask

How do you check if a string contains a substring ignoring case?

One of the easiest ways to check if a String has a substring without considering the case is to convert all the Strings to lowercase and then check for a substring. To check for a substring, we use the contains() method, and for converting the String to lowercase, we use the toLowerCase() method.

Is string contain case sensitive?

Yes, contains is case sensitive. You can use java. util.

Does == ignore case?

Java String: equalsIgnoreCase() MethodTwo strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.


1 Answers

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.

like image 75
Marcel Gosselin Avatar answered Sep 19 '22 15:09

Marcel Gosselin