在c++中查找字符串中是否含有特定字符
可以使用 STL 中的 find() 函数。
示例代码:
#include <iostream>
#include <string>
int main() {
std::string str = "Hello World!";
char c = 'o';
if (str.find(c) != std::string::npos) {
std::cout << "The string contains the character " << c << std::endl;
} else {
std::cout << "The string does not contain the character " << c << std::endl;
}
return 0;
}
输出结果:
The string contains the character o
原文地址: https://www.cveoy.top/t/topic/bcxT 著作权归作者所有。请勿转载和采集!