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?
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.
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 .
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 ".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With