假设我有一些字符串,str。
我要检查str是否包含关键字:“ samples”。但是,“ samples”可以采用任何大写形式,例如:“ Samples”,“ SamPleS”,“ SAMPLES”。
这是我正在尝试的:
string str = "this is a FoO test";
if (str.find("foo") != std::string::npos){
std::cout << "WORKS";
}
这不会检测到“ FoO”子字符串。我可以通过某种方式忽略大写吗?还是我应该完全使用其他东西?
将两个字符串都转换为大写:
std::string upperCase(std::string input) {
for (std::string::iterator it = input.begin(); it != input.end(); ++ it)
*it = toupper((unsigned char)*it);
return input;
}
然后使用find()
:
upperCase(str).find(upperCase(target))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句