find

Syntax:

    #include <string>
    size_type find( const string& str, size_type index = 0 ) const;
    size_type find( const Char* str, size_type index = 0 ) const;
    size_type find( const Char* str, size_type index, size_type length ) const;
    size_type find( Char ch, size_type index = 0 ) const;

The find method returns either:

  1. the first occurrence of str within the current string, starting at index, or string::npos if nothing is found
  2. the first length characters of str within the current string, starting at index, or string::npos if nothing is found.

For example:

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

The result is

Didn't find Omega
Found Gamma at 11

Related Topics: find_first_not_of, find_first_of, find_last_not_of, find_last_of, rfind