gpt4 book ai didi

c++ - 如何在 C++ 中执行 std::string indexof 返回匹配字符串的索引?

转载 作者:IT老高 更新时间:2023-10-28 21:55:02 31 4
gpt4 key购买 nike

我正在从 std 命名空间中寻找一个字符串 indexof 函数,它返回一个匹配字符串的整数,类似于同名的 java 函数。比如:

std::string word = "bob";
int matchIndex = getAString().indexOf( word );

getAString() 的定义如下:

std::string getAString() { ... }

最佳答案

试试 find功能。

这是我链接的文章中的示例:

 string str1( "Alpha Beta Gamma Delta" );
string::size_type loc = str1.find( "Omega", 0 );
if( loc != string::npos ) {
cout << "Found Omega at " << loc << endl;
} else {
cout << "Didn't find Omega" << endl;
}

关于c++ - 如何在 C++ 中执行 std::string indexof 返回匹配字符串的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/651497/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com