C++ 如何判断一个字符串是否包含在另一个字符串中
C++代码。 例子: x:“这是我的第一个节目”; y:“我的”C++ 如何判断一个字符串是否包含在另一个字符串中,c++,string,C++,String,C++代码。 例子: x:“这是我的第一个节目”; y:“我的” 您可以使用执行此操作。您可以使用std::string::find() 函数的编写方式取决于对空字符串的搜索是否成功。 如果考虑在任何字符串中存在空字符串,则函数将看起来为 bool function( const std::string &x, const std::string string &y ) { return ( x.find( y ) != std::string::npos ); } 如
您可以使用执行此操作。您可以使用
std::string::find()
函数的编写方式取决于对空字符串的搜索是否成功。 如果考虑在任何字符串中存在空字符串,则函数将看起来为
bool function( const std::string &x, const std::string string &y )
{
return ( x.find( y ) != std::string::npos );
}
如果考虑空字符串的搜索返回false,则函数将看起来为
bool function( const std::string &x, const std::string string &y )
{
return ( !y.empty() && x.find( y ) != std::string::npos );
}
我希望函数对空字符串返回false。或
std::search
,这将适用于任何序列。堆栈溢出不是神奇的代码生成器。
bool function( const std::string &x, const std::string string &y )
{
return ( x.find( y ) != std::string::npos );
}
bool function( const std::string &x, const std::string string &y )
{
return ( !y.empty() && x.find( y ) != std::string::npos );
}