#include<iostream>
using namespace std;
int main(){
string a="asdfghjkl";
string b="asdfg";
//C语言 strstr() 如果b是a的字串,返回指针位置;否则返回nullptr
if(strstr(a.c_str(),b.c_str())!=nullptr){
cout<<"b是a的字串"<<endl;
}
else{
cout<<"b不是a的字串"<<endl;
}
//C++ find() 在字符串a中查找是否存在字符串b,如果存在,返回起始位置(string::size_type类型);不存在返回 string::npos
if(a.find(b)!=string::npos ){
cout<<"b是a的字串"<<endl;
}
else{
cout<<"b不是a的字串"<<endl;
}
return 0;
}
C/C++判断字符串是否为另一字符串的子字符串
最新推荐文章于 2024-01-15 22:38:27 发布