For comparing any strings without considering their case, there are various C library functions such as strcasecmp()
, stricmp()
and stricmpi()
. What is the difference between these?
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.
The strcasecmp() function compares, while ignoring differences in case, the string pointed to by string1 to the string pointed to by string2.
Description. The strcasecmp() function compares string1 and string2 without sensitivity to case. All alphabetic characters in string1 and string2 are converted to lowercase before comparison. The strcasecmp() function operates on null terminated strings.
To make strcmp case-insensitive, use strcasecmp from #include <strings. h> . strcasecmp can be used in exactly the same way as strcmp. To make strncmp case-insensitive, use strncasecmp from #include <strings.
There are multiple ways to do many things primarily because the standards process lags behind implementations. People see the need for a function (in this case, case insensitive string comparison) and some compiler writers/library writers implement a function called strcmpi
, while another group implements stricmp
, while another group decides it is not necessary to implement it, while another group implements strcasecmp
while another group implements strcmpnocase
, etc. Years later, representatives from the various groups meet in mortal combat and the winner's implementation becomes part of the language. Meanwhile, the other implementations continue with the other-named methods and the language grows stronger/fragments/gains bloat (depending on your point of view).
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