find

C/C++ Reference

find
Syntax:
  #include <string>
  size_type find( const string& str, size_type index );
  size_type find( const char* str, size_type index );
  size_type find( const char* str, size_type index, size_type length );
  size_type find( char ch, size_type index );

The function find() either:

  • returns the first occurrence of str within the current string, starting at index, string::npos if nothing is found,
  • if the length parameter is given, then find() returns the first occurrence of the first length characters of str within the current string, starting at index, string::npos if nothing is found,
  • or returns the index of the first occurrence ch within the current string, starting at index, string::npos if nothing is found.

For example:

   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;         
   }
Related topics: