如何在 C++ 中执行 std::string indexof 返回匹配字符串的索引?
我正在寻找 std 命名空间中的字符串 indexof 函数,该函数返回类似于同名 java 函数的匹配字符串的整数。 类似于:
std::string word = "bob";
int matchIndex = getAString().indexOf( word );
其中 getAString() 定义如下:
std::string getAString() { ... }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用
find
函数。这是我链接的文章中的示例:
Try the
find
function.Here is the example from the article I linked:
您正在寻找
std::basic_string<>
函数模板:如果未找到字符串,则返回索引或
std::string::npos
。You are looking for the
std::basic_string<>
function template:This returns the index or
std::string::npos
if the string is not found.从您的示例中不清楚您要在哪个字符串中搜索“bob”,但以下是如何使用 查找。
It's not clear from your example what String you're searching for "bob" in, but here's how to search for a substring in C++ using find.
我不太确定你的示例意味着什么,但对于 stl string 类,请查看 查找 和 r查找
I'm not exactly sure what your example means, but for the stl string class, look into find and rfind