使用 string 的 find 成员函数。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "afdsdfs_hello_sdfas#@!";
string str1 = "hello";
string::size_type idx = str.find( str1 );
if ( idx != string::npos )
{
cout << "字符串含有“<< str1 << "\n";
}
else
{
cout << "字符串没有" << str1 << "\n";
}
}
解析:string::npos是个返回值