std::string::find()无法将std::string用作参数
std::string::find() not working with std::string as argument
我在使用std::string::find()
时遇到了一些对我来说没有多大意义的东西。我希望这里的人能够发现我的错误:
std::string testString = "PFAIL";
std::string response = "PFAILn";
if( response.find(testString) != std::string::npos )
{
//do something
}
由于某种原因,此代码从未命中//do something
注释。有什么想法吗?
编辑:我的意思是它永远不会到达//do something
代码块,如果我用以下方式表达它,它应该这样做:
if( response.find( testString.c_str() ) != std::string::npos )
{
// do something
}
也许您应该考虑在if语句中使用直接逻辑:
if( response.find(testString) == std::string::npos )
这个合乎逻辑的表达可能会减少你的困惑。如果find的返回值等于std::string::npos
,则在response
字符串中未找到testString
变量中的字符串。
伊迪丝:我的逻辑回答错了。
您的情况与预期的情况不符。如果未找到任何内容,则返回npos
。由于响应中明确包含测试,因此不会返回npos
,并且会输入if
的正文。
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- 使用 std::string () const 函数启动线程或未来
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 如何更改大小(std::string)
- std::string 的对象真的可以移动吗?
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- 真的没有来自 std::string_view 的 std::string 的显式构造函数吗?
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 重载 + 自己的类和 std::string 的运算符
- 如何使用 std::string 作为 QHash 的键?
- 将日语 wstring 转换为 std::string
- 可以从std::string继承以提供类型一致性吗
- 构造函数采用std::string_view与std::string并移动
- 在共享缓冲区内存中创建 ::std::string 对象
- std::string.size() 未知行为
- Valgrind 在 std::string::swap 中报告 SIGILL