试试这个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; }
从你的例子中不清楚你在搜索"bob"中的String,但是这里是如何使用find在C++中搜索子字符串.
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; }