使用 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是个返回值
string里find()函数,判断字符串是否包含某个子串
最新推荐文章于 2023-08-21 20:23:25 发布