How can I make the following case insensitive?
myString1.Contains("AbC")
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.
The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.
Also, TreeMap uses a Comparator to find if an inserted key is a duplicate or a new one. Therefore, if we provide a case-insensitive String Comparator, we'll get a case-insensitive TreeMap. which we can supply in the constructor: Map<String, Integer> treeMap = new TreeMap<>(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.
You can create your own extension method to do this:
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source != null && toCheck != null && source.IndexOf(toCheck, comp) >= 0;
}
And then call:
mystring.Contains(myStringToCheck, StringComparison.OrdinalIgnoreCase);
You can use:
if (myString1.IndexOf("AbC", StringComparison.OrdinalIgnoreCase) >=0) {
//...
}
This works with any .NET version.
bool b = list.Contains("Hello", StringComparer.CurrentCultureIgnoreCase);
[EDIT] extension code:
public static bool Contains(this string source, string cont
, StringComparison compare)
{
return source.IndexOf(cont, compare) >= 0;
}
This could work :)
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