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;
}
最后修改:2023 年 02 月 14 日
如果觉得我的文章对你有用,请随意赞赏