find_first_of [C++ Reference]
Keywords: string,find_first_of
Date: 2009-10-10T20:07:50-0700

## find_first_of

Syntax:

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

The find_first_of function either:

- returns the index of the first character within the current string that matches any character in `str`, beginning the search at `index`, string::npos if nothing is found,
- searches the current string, beginning at `index`, for any of the first `num` characters in `str`, returning the index in the current string of the first character found, or string::npos if no characters match,
- or returns the index of the first occurrence of `ch` in the current string, starting the search at `index`, string::npos if nothing is found.

For example, the following code uses find_first_of to replace all the vowels in a string with asterisks:

      string str = "In this house, we obey the laws of thermodynamics!";
      string::size_type found = str.find_first_of("aeiouAEIOU");

      while( found != string::npos ) {
        str[found] = '*';
        found = str.find_first_of("aeiouAEIUO",found+1);
      }

      cout << str << '\n';  // displays "*n th*s h**s*, w* *b*y th* l*ws *f th*rm*dyn*m*cs!"

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