Case-insensitive string comparison in C++
Boost includes a handy algorithm for this:
#include <boost/algorithm/string.hpp>
// Or, for fewer header dependencies:
//#include <boost/algorithm/string/predicate.hpp>
std::string str1 = "hello, world!";
std::string str2 = "HELLO, WORLD!";
if (boost::iequals(str1, str2))
{
// Strings are identical
}
How to do case insensitive string comparison?
The simplest way to do it (if you're not worried about special Unicode characters) is to call toUpperCase
:
var areEqual = string1.toUpperCase() === string2.toUpperCase();
How to do a case-insensitive string comparison?
If you can afford deviating a little from strict C
standard, you can make use of strcasecmp()
.
It is a POSIX API.
Otherwise, you always have the option to convert the strings to a certain case (UPPER or lower) and then perform the normal comparison using strcmp()
.
How do I do a case-insensitive string comparison?
Assuming ASCII strings:
string1 = 'Hello'
string2 = 'hello'
if string1.lower() == string2.lower():
print("The strings are the same (case insensitive)")
else:
print("The strings are NOT the same (case insensitive)")
As of Python 3.3, casefold() is a better alternative:
string1 = 'Hello'
string2 = 'hello'
if string1.casefold() == string2.casefold():
print("The strings are the same (case insensitive)")
else:
print("The strings are NOT the same (case insensitive)")
If you want a more comprehensive solution that handles more complex unicode comparisons, see other answers.
How to compare the string with case insensitive
I could be missing something but I think the simplest solution is just to use the lower()
function.
s = "PRAVEEN"
a = "hello how are you,i am praveen"
# will return true if s in a (regardless of original upper/lower cases)
if s.lower() in a.lower():
print "s in a"
You could write a function for it if you have to do it many times?
def isInString(substring, originalString):
if substring.lower() in originalString.lower():
return true
else:
return false
Is there a C# case insensitive equals operator?
Try this:
string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
Ignore case in string comparison
This is the most pythonic I can think of. Better to ask for foregiveness than for permission:
>>> def iequal(a, b):
... try:
... return a.upper() == b.upper()
... except AttributeError:
... return a == b
...
>>>
>>> iequal(2, 2)
True
>>> iequal(4, 2)
False
>>> iequal("joe", "Joe")
True
>>> iequal("joe", "Joel")
False
Case insensitive string comparison in Go
https://golang.org/pkg/strings/#EqualFold is the function you are looking for. It is used like this (example from the linked documentation):
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.EqualFold("Go", "go"))
}
Related Topics
Populate Another Select Dropdown from Database Based on Dropdown Selection
Unexpected Character in Input: '\' (Ascii=92) State=1
Casting an Array with Numeric Keys as an Object
PHP Recursion to Get All Possibilities of Strings
Why Should I Use Templating System in PHP
Email from PHP Has Broken Subject Header Encoding
Sqlsrv_Num_Rows Not Returning Any Value
PHP Exec $Path Variable Missing Elements
Custom User Authentication Base on the Response of an API Call
Checking If Array Is Multidimensional or Not
How to Install the Ext-Curl Extension with PHP 7
How to Easily Consume a Web Service from PHP
PHP Messing with HTML Charset Encoding
How to Extract Images from a PDF File