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
}
Answer from Rob on Stack OverflowBoost
boost.org › doc › libs › 1_48_0 › boost › algorithm › string › compare.hpp
boost/algorithm/string/compare.hpp
This is a less restrictive version which wraps operator ==. */ struct is_equal { //! Function operator /*! Compare two operands for equality */ template< typename T1, typename T2 > bool operator()( const T1& Arg1, const T2& Arg2 ) const { return Arg1==Arg2; } }; //! case insensitive version of is_equal /*! Case insensitive comparison predicate.
Top answer 1 of 16
336
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
}
2 of 16
157
The trouble with boost is that you have to link with and depend on boost. Not easy in some cases (e.g. android).
And using char_traits means all your comparisons are case insensitive, which isn't usually what you want.
This should suffice. It should be reasonably efficient. Doesn't handle unicode or anything though.
#include <cctype> // std::tolower
#include <algorithm> // std::equal
bool ichar_equals(char a, char b)
{
return std::tolower(static_cast<unsigned char>(a)) ==
std::tolower(static_cast<unsigned char>(b));
}
bool iequals(const std::string& a, const std::string& b)
{
return a.size() == b.size() &&
std::equal(a.begin(), a.end(), b.begin(), ichar_equals);
}
C++14 version
#include <cctype> // std::tolower
#include <algorithm> // std::equal
bool iequals(const std::string& a, const std::string& b)
{
return std::equal(a.begin(), a.end(), b.begin(), b.end(), ichar_equals);
}
C++20 version using std::ranges
#include <cctype> // std::tolower
#include <algorithm> // std::equal
#include <string_view> // std::string_view
bool iequals(std::string_view lhs, std::string_view rhs)
{
return std::ranges::equal(lhs, rhs, ichar_equals);
}
Narkive
boost-users.boost.narkive.com › 2RCmYTwX › case-insensitive-less-than-string-comparison
case insensitive less_than string comparison
He's looking for something like is_iless, which I think would be a nice addition too string_algo. I see. Sorry about that. Somehow I have overlooked this obvious aspect. You are right, such a function can be usefull. I will add it (but it will have to wait after the release) Any thoughts on carrying this one step further. I've the case where I need not just case insensitivity, but more of an interleaved case sensitivity. For example using the following: int CompareInterleavedCaseChar( char a1, char a2 ) { if( std::toupper(a1) < std::toupper(a2) ) return -1; else if( std::toupper(a2) < std::toupper(a1) ) return 1; else if( a1 < a2 ) return -1; else if( a2 < a1 ) return 1; else return 0; } results in the following ordering: ABCdef ABcDef AbcDef Abcdef in other words A < a < B < b < C ...
Boost
boost.org › doc › libs › 1_45_0 › boost › algorithm › string › compare.hpp
boost/algorithm/string/compare.hpp - 1.45.0
This is a less restrictive version which wraps operator ==. */ struct is_equal { //! Function operator /*! Compare two operands for equality */ template< typename T1, typename T2 > bool operator()( const T1& Arg1, const T2& Arg2 ) const { return Arg1==Arg2; } }; //! case insensitive version of is_equal /*! Case insensitive comparison predicate.
Boost
boost.org › doc › libs › latest › libs › beast › doc › html › beast › ref › boost__beast__iequals.html
Boost
Returns true if two strings are equal, using a case-insensitive comparison.
Reddit
reddit.com › r/cpp_questions › what's the best way to compare strings no matter if it has upper or lower case?
r/cpp_questions on Reddit: What's the best way to compare strings no matter if it has upper or lower case?
April 13, 2021 -
I have seen some ways on the internet on how to do it, but what is the best way? What should I use?
Top answer 1 of 10
28
There is two options in general: Transform to lower (or upper) case, then compare. Compare letter by letter, but if the initial compare fails, transform to lower (or upper) case In the first case, the comparison may be SIMDed (which really is only relevant for large strings) and the code is more readable out of the box In the second case, you are doing manual work but potentially do less transforms. What to do depends on the application. Are you comparing two runtime determined strings? Or comparing a string against a literal?
2 of 10
7
There is no best, there is only what makes sense for your use case. You can make a custom char traits that is case insensitive, or just a custom comparator that is case insensitive. Do you want the whole string to be case insensitive or just the comparison? I haven't looked at string_view yet, but I suspect you can do something with that and make a case insensitive string view.
O'Reilly
oreilly.com › library › view › c-cookbook › 0596007612 › ch04s14.html
4.13. Doing a Case-Insensitive String Comparison - C++ Cookbook [Book]
November 8, 2005 - 1 #include <string> 2 #include <iostream> 3 #include <algorithm> 4 #include <cctype> 5 #include <cwctype> 6 7 using namespace std; 8 9 inline bool caseInsCharCompareN(char a, char b) { 10 return(toupper(a) == toupper(b)); 11 } 12 13 inline bool caseInsCharCompareW(wchar_t a, wchar_t b) { 14 return(towupper(a) == towupper(b)); 15 } 16 17 bool caseInsCompare(const string& s1, const string& s2) { 18 return((s1.size() == s2.size()) && 19 equal(s1.begin(), s1.end(), s2.begin(), caseInsCharCompareN)); 20 } 21 22 bool caseInsCompare(const wstring& s1, const wstring& s2) { 23 return((s1.size() == s2.size()) && 24 equal(s1.begin(), s1.end(), s2.begin(), caseInsCharCompareW)); 25 } 26 27 int main() { 28 string s1 = "In the BEGINNING..."; 29 string s2 = "In the beginning..."; ...
Authors D. Ryan StephensChristopher Diggins…
Published 2005
Pages 594
Cplusplus
cplusplus.com › forum › beginner › 243351
Comparing strings regardless of case - C++ Forum
Sadly, there isn't really a built-in way to do case-insensitive string compare. At least not one that I know of. I suggest writing your own function, e.g. "iequals(a, b)" and convert each character to lower before comparing.
Boost
boost.org › doc › libs › 1_34_0 › boost › algorithm › string › predicate.hpp
boost/algorithm/string/predicate.hpp
Elements are compared case insensitively \param Arg1 First argument \param Arg2 Second argument \return The result of the test \note This function provides the strong exception-safety guarantee */ template · inline bool ilexicographical_compare( const Range1T& Arg1, const Range2T& Arg2) { return std::lexicographical_compare( begin(Arg1), end(Arg1), begin(Arg2), end(Arg2), is_iless()); } // all predicate -----------------------------------------------// //! 'All' predicate /*! This predicate holds it all its elements satisfy a given condition, represented by the predicate.
Cplusplus
cplusplus.com › forum › general › 86290
toupper compare array to string - C++ Forum
... There's also boost::algorithm::to_upper(input) demo: http://ideone.com/zkIE2x ref: http://www.boost.org/doc/libs/release/doc/html/boost/algorithm/to_upper.html As for case-insensitive comparison, there's boost::iequals(str1, str2)) demo (comparing array and string, as in OP): ...
Boost
boost.org › doc › libs › 1_41_0 › doc › html › boost › algorithm › istarts_with.html
Function template istarts_with - 1.41.0
boost::algorithm::istarts_with ... Range1T & Input, const Range2T & Test, const std::locale & Loc = std::locale()); This predicate holds when the test string is a prefix of the Input....
TutorialsPoint
tutorialspoint.com › case-insensitive-string-comparison-in-cplusplus
Case-insensitive string comparison in C++
This is a simple and straightforward approach for case-insensitive string comparison. Here the idea is to convert both the strings to lowercase or uppercase before comparing them.
Packtpub
subscription.packtpub.com › book › cloud-and-networking › 9781787282247 › 7 › 07lvl1sec74 › changing-cases-and-case-insensitive-comparison
Boost C++ Application Development Cookbook - Second ...
Access over 7,500 Programming & Development eBooks and videos to advance your IT skills. Enjoy unlimited access to over 100 new titles every month on the latest technologies and trends
Mitk
mitk.org › images › 3 › 3e › BugSquashingSeminars$13-11-06-bugsquashing-StringComparison.pdf pdf
11/14/13 (Case Insensitive) String Comparison in C++ Adrian Winterstein
std::string str2 = "Foo"; How to compare them case insensitive? The ==operator will not match. if ( boost::iequals(str1, str2) ) MITK_INFO << "Strings are equal."; Boost offers a solution: Page 5 · 11/14/13 | Adrian Winterstein · Departement MBI ·
Boost
boost.org › doc › libs › 1_85_0 › boost › algorithm › string › compare.hpp
boost/algorithm/string/compare.hpp - 1.85.0
This is a less restrictive version which wraps operator ==. */ struct is_equal { //! Function operator /*! Compare two operands for equality */ template< typename T1, typename T2 > bool operator()( const T1& Arg1, const T2& Arg2 ) const { return Arg1==Arg2; } }; //! case insensitive version of is_equal /*! Case insensitive comparison predicate.
Boost
boost.org › doc › libs › 1_33_1 › boost › algorithm › string › compare.hpp
boost/algorithm/string/compare.hpp - 1.33.1
This is a less restrictive version which wraps operator ==. */ struct is_equal { //! Function operator /*! Compare two operands for equality */ template< typename T1, typename T2 > bool operator ()( const T1& Arg1, const T2& Arg2 ) const { return Arg1==Arg2; } }; //! case insensitive version of is_equal /*! Case insensitive comparison predicate.