I am trying to use boost string algorithms for case insensitive search.
total newbie here.
if I am using it this way, I get an error.
std::string str1("Hello world");
std::string str2("hello");
if ( boost::ifind_first(str1, str2) ) some code;
Converting to char pointers resolves the problem.
boost::ifind_first( (char*)str1.c_str(), (char*)str2.c_str() );
Is there a way to search std::string objects directly?
Also, maybe there is another way to know if string is present inside another string with case-insensitive search?
You need to use boost::iterator_range. This works:
typedef const boost::iterator_range<std::string::const_iterator> StringRange;
std::string str1("Hello world");
std::string str2("hello");
if ( boost::ifind_first(
StringRange(str1.begin(), str1.end()),
StringRange(str2.begin(), str2.end()) ) )
std::cout << "Found!" << std::endl;
EDIT: Using a const iterator_range in the typedef allows passing a temporary range.
The more simplicity way
std::string str1("Hello world");
std::string str2("hello");
if (boost::icontains(str1, str2)) {
...
}
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