如何判断一个字符串是否包含在另一个字符串中
how to tell if a string is contained in another string
C++代码。示例:x: "这是#我的第一个程序";y: "#my";
bool function(string x, string y)
{
//Return true if y is contained in x
return ???;
}
您可以使用std::string::find()
bool function(string x, string y)
{
return (x.find(y) != std::string::npos);
}
您可以使用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。
相关文章:
- 编译包含字符串的代码时遇到问题
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- C++ 创建包含链表和字符串的对象的链接列表时出错
- C++-字符串是否包含一个带有简单循环的单词
- 编辑字符串以包含括号 c++ 之间的变量
- 使用提升元解析字符串解析包含的文件
- 当字符串包含 %2C 时,在 c++ 中不完整的 std::string
- C++ 检查 UTF8 字符串包含指定的字符
- 将文件作为字符串包含在内
- 为什么string.empty()在字符串包含一个值时评估为true
- boost::p rogram_option::store 在选项字符串包含混合语言字符时引发异常
- 在 c++ 中将字符串(包含字符和浮点数)转换为双精度
- 将字符串(包含数字)转换为整数并返回该整数
- 如果输入字符串包含"%",则vsntprintf_s崩溃
- 当一个字符串包含另一个字符串的一部分时比较字符串
- 如果字符串包含多个 ,如何在每25个换行符( )上分割字符串
- 输出字符串包含奇怪的字符
- 当字符串包含多个NUL字符时,如何获取c_str(用于发送)
- 乘法的字符串[包含整数],输出也存储在字符串中,如何