是否存在一个不区分大小写的find()
方法适用于std::string
?
是否存在一个不区分大小写的find()
方法适用于std::string
?
你可以将两个字符串都转换为大写,然后使用普通的查找函数。(注意:如果你有Unicode字符串,这种方法可能不正确。)
在Boost中还有一个针对大小写不敏感搜索的ifind_first
函数(请注意它返回一个范围而不是一个size_t
值)。
#include <string>
#include <boost/algorithm/string/find.hpp>
#include <cstdio>
#include <cctype>
std::string upperCase(std::string input) {
for (std::string::iterator it = input.begin(); it != input.end(); ++ it)
*it = toupper(*it);
return input;
}
int main () {
std::string foo = "1 FoO 2 foo";
std::string target = "foo";
printf("string.find: %zu\n", foo.find(target));
printf("string.find w/ upperCase: %zu\n", upperCase(foo).find(upperCase(target)));
printf("ifind_first: %zu\n", boost::algorithm::ifind_first(foo, target).begin() - foo.begin());
return 0;
}
libicu
(在这些情况下,您可能需要一个区域设置来了解土耳其语中的İ)。 - kennytm#include <iostream>
#include <algorithm>
#include <string>
bool lower_test (char l, char r) {
return (std::tolower(l) == std::tolower(r));
}
int main()
{
std::string text("foo BaR");
std::string search("bar");
std::string::iterator fpos = std::search(text.begin(), text.end(), search.begin(), search.end(), lower_test);
if (fpos != text.end())
std::cout << "found at: " << std::distance(text.begin(), fpos) << std::endl;
return 0;
}
for(int i=0; i<yourString.length()
&& tolower(yourString[i])!=yourLoweredChar; i++)
{
return i;
}
return -1;
最高效的方式
性能保证是线性的,初始化成本为2 * NEEDLE_LEN比较。(glic)
#include <cstring>
#include <string>
#include <iostream>
int main(void) {
std::string s1{"abc de fGH"};
std::string s2{"DE"};
auto pos = strcasestr(s1.c_str(), s2.c_str());
if(pos != nullptr)
std::cout << pos - s1.c_str() << std::endl;
return 0;
}
strcasestr
不是标准的 C++ 的一部分。 - EvgStrStrIA
,并且加上#pragma comment(lib,"shlwapi.lib")
。 - Delta