Case Insensitive 'In'

How do I do a case-insensitive string comparison?

Assuming ASCII strings:

string1 = 'Hello'
string2 = 'hello'

if string1.lower() == string2.lower():
print("The strings are the same (case insensitive)")
else:
print("The strings are NOT the same (case insensitive)")

As of Python 3.3, casefold() is a better alternative:

string1 = 'Hello'
string2 = 'hello'

if string1.casefold() == string2.casefold():
print("The strings are the same (case insensitive)")
else:
print("The strings are NOT the same (case insensitive)")

If you want a more comprehensive solution that handles more complex unicode comparisons, see other answers.

Postgres Case Insensitive in IN operator?

You can try to use ILIKE with ANY

SELECT * 
FROM fruits
WHERE name ILIKE ANY(array['Orange', 'grape', 'APPLE', 'ManGO']);

sqlfiddle

Case insensitive list filter

You can use str.casefold.

filter_object = list(filter(lambda a: f_val.casefold() in a.casefold(), f_Results))

Make the string case insensitive in java

To count words in paragraph with case insensitive we also used Pattern class with while loop:

For example:

public class CountWordsInParagraphCaseInsensitive {

public static void main(String[] args) {
StringBuilder paragraph = new StringBuilder();
paragraph.append("I am at office right now.")
.append("I love to work at oFFicE.")
.append("My OFFICE located at center of kathmandu valley");
String searchWord = "office";
Pattern pattern = Pattern.compile(searchWord, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(paragraph);
int count = 0;
while (matcher.find())
count++;
System.out.println(count);

}

}

javascript includes() case insensitive

You can create a RegExp from filterstrings first

var filterstrings = ['firststring','secondstring','thridstring'];
var regex = new RegExp( filterstrings.join( "|" ), "i");

then test if the passedinstring is there

var isAvailable = regex.test( passedinstring ); 

How to make string inputs case insensitive in C#?

You should try

if (boyName.ToUpper() == "Bobby".ToUpper())

How to do a case-insensitive string comparison?

If you can afford deviating a little from strict C standard, you can make use of strcasecmp().
It is a POSIX API.

Otherwise, you always have the option to convert the strings to a certain case (UPPER or lower) and then perform the normal comparison using strcmp().



Related Topics



Leave a reply



Submit