Possible Duplicate:
Case insensitive contains(string)
With Contains()
method of String class a substring can be found. How to find a substring in a string in a case-insensitive manner?
It's a cousin to the standard C library string comparison function, strstr(). The word case sandwiched inside means that the strings are compared in a case-insensitive way. Otherwise, both functions are identical.
Ignore case : check if a string exists in another string in case insensitive approach. Use re.search() to find the existence of a sub-string in the main string by ignoring case i.e. else return a tuple of False & empty string.
Comparing strings in a case insensitive manner means to compare them without taking care of the uppercase and lowercase letters. To perform this operation the most preferred method is to use either toUpperCase() or toLowerCase() function.
You can use the IndexOf() method, which takes in a StringComparison type:
string s = "foobarbaz"; int index = s.IndexOf("BAR", StringComparison.CurrentCultureIgnoreCase); // index = 3
If the string was not found, IndexOf() returns -1.
There's no case insensitive version. Use IndexOf
instead (or a regex though that is not recommended and overkill).
string string1 = "my string"; string string2 = "string"; bool isContained = string1.IndexOf(string2, StringComparison.OrdinalIgnoreCase) >= 0;
StringComparison.OrdinalIgnoreCase
is generally used for more "programmatic" text like paths or constants that you might have generated and is the fastest means of string comparison. For text strings that are linguistic use StringComparison.CurrentCultureIgnoreCase
or StringComparison.InvariantCultureIgnoreCase
.
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