ken*_*ytm 12
您可以将两个字符串置于大写并使用常规查找.(注意:如果您有Unicode字符串,这种方法可能不正确.)
在Boost中,还有ifind_first一些不区分大小写的搜索.(请注意,它返回的是范围而不是a 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;
}
Run Code Online (Sandbox Code Playgroud)
这就是我的建议,(与@programmersbook相同)
#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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16160 次 |
| 最近记录: |