How to do std::string indexof in C++ that returns index of matching string?

Try the find function.

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;
 }

Leave a Comment