C++ 判断字符串是否包含子字符串 - std::string::find 函数
您可以使用std::string类的find函数来判断一个字符串中是否包含另一个字符串。以下是一个示例代码:
#include <iostream>
#include <string>
int main() {
std::string name = 'xixi is my name';
if (name.find('xixi') != std::string::npos) {
std::cout << 'name contains 'xixi'' << std::endl;
} else {
std::cout << 'name does not contain 'xixi'' << std::endl;
}
return 0;
}
在这个例子中,我们使用find函数来搜索字符串name中是否包含子字符串'xixi'。如果find函数返回的值不等于std::string::npos,则表示找到了该子字符串。如果找到了,就输出字符串name contains 'xixi',否则输出字符串name does not contain 'xixi'。
原文地址: https://www.cveoy.top/t/topic/qjCk 著作权归作者所有。请勿转载和采集!