如何做std :: string indexof在C ++中返回匹配字符串的索引? [英] How to do std::string indexof in C++ that returns index of matching string?
本文介绍了如何做std :: string indexof在C ++中返回匹配字符串的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个字符串indexof函数从std命名空间,返回一个匹配的字符串类似于同名的java函数的整数。类似:
I'm looking for a string indexof function from the std namespace that returns an integer of a matching string similar to the java function of the same name. Something like:
std::string word = "bob";
int matchIndex = getAString().indexOf( word );
其中getAString()的定义如下:
where getAString() is defined like this:
std::string getAString() { ... }
推荐答案
尝试 查找
功能。
以下是我链接的文章的示例:
Here is the example from the article I linked:
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;
}
这篇关于如何做std :: string indexof在C ++中返回匹配字符串的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文