public member function

std::string::find_first_of

<string>
size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;
Find character in string
Searches the string for any of the characters that are part of either str, s or c, and returns the position of the first occurrence in the string.

When pos is specified the search only includes characters on or after position pos, ignoring any possible occurrences at previous character positions.

Notice that for a match to happen it is enough that one of the characters matches in the string (any of them). To search for an entire sequence of characters use find instead.

Parameters

str
string containing the characters to search for in the object. The first character in the string that compares equal to any of the characters in str is considered a match.
s
Array with a sequence of characters. The first character in the string that compares equal to any of the characters in this sequence is considered a match.
In the second member function version, the number of characters in the sequence of characters to search for is only determined by parameter n.
In the third version, a null-terminated sequence (c-string) is expected, and the amount of characters to search for is determined by its length, which is indicated by a null-character after the last character.
n
Length of sequence of characters to search for.
c
Individual character to be searched for.
pos
Position of the first character in the string to be taken into consideration for possible matches. A value of 0 means that the entire string is considered.

Return Value

The position of the first occurrence in the string of any of the characters searched for.
If the content is not found, the member value npos is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// string::find_first_of
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("Replace the vowels in this sentence by asterisks.");
  size_t found;

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

  cout << str << endl;

  return 0;
}


R*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.

Basic template member declarations

( basic_string<charT,traits,Allocator> )
1
2
3
4
5
6
7
8
typedef typename Allocator::size_type size_type;
size_type find_first_of ( const basic_string& str,
                           size_type pos = 0 ) const;
size_type find_first_of ( const charT* s, size_type pos,
                           size_type n ) const;
size_type find_first_of ( const charT* s,
                           size_type pos = 0 ) const;
size_type find_first_of ( charT c, size_type pos = 0 ) const;


See also