C++中可以使用string.find()
函数来判断子串是否存在:
// 定义字符串
string str = "This is a string";
// 查找子串
size_t found = str.find("string");
// 如果找到,found的值将不等于string::npos
if (found != string::npos)
{
cout << "Substring found at index: " << found << endl;
}
else
{
cout << "Substring not found" << endl;
}