Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make IEnumerable<string>.Contains case-insensitive?

Suppose I have a .net Array of strings.

string[] strings = new string[] { "AbC", "123", "Xyz", "321" }; 

If I wanted to see if the array of strings contains "ABC", I could write

strings.Contains("ABC"); 

However, suppose that I want a function that will return true if the uppercase values of the strings contain "ABC". I could uppercase the entire array, but it looks like the .Contains method has some overloads for specifying the comparison, but I'm confused by the syntax.

How can I use the IEnumerable<string>.Contains() method implement this logic?

like image 693
Vivian River Avatar asked Feb 08 '13 21:02

Vivian River


People also ask

Is string contains case sensitive in C#?

The string. Contains() method in C# is case sensitive. And there is not StringComparison parameter available similar to Equals() method, which helps to compare case insensitive.

How do I check if a string contains a substring case insensitive?

Find Case Insensitive Substring in a String in Java In this example, we used the Pattern class and its compile() , matcher() , and find() methods to check whether a string contains a substring or not. We used CASE_INSENSITIVE , which returns a boolean value, either true or false .

Is Linq case sensitive?

LINQ StartsWith , EndsWith , and Contains are case sensitive and return false if two same string s are of different cases, e.g., " STRING " and " string ".


1 Answers

Use overloaded Enumerable.Contains method which accepts equality comparer:

strings.Contains("ABC", StringComparer.InvariantCultureIgnoreCase) 

Also there is strings comparer in box which you can use.

like image 183
Sergey Berezovskiy Avatar answered Sep 22 '22 10:09

Sergey Berezovskiy