C++ String::find_first_of() function



The C++ std::string::find_first_of is used to locate the first occurrence of any character from a specified set within a string. It searches for the first instance of any character found in a given string or character set and returns the position of that character. If no character is found , it returns std::string::npos.

Syntax

Following is the syntax for std::string::find_first_of() function.

size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
or	
size_t find_first_of (const char* s, size_t pos = 0) const;
or
size_t find_first_of (const char* s, size_t pos, size_t n) const;
or
size_t find_first_of (char c, size_t pos = 0) const noexcept;

Parameters

  • str − It indicates the another string with the characters to search for.
  • pos − It indicates the position of the first character in the string to be considered in the search.
  • s − It indicates the pointer to an array of characters.
  • n − It indicates the number of character values to search for.
  • c − It indicates the individual character to be searched for.

Return value

This function returns the position of the searched character.

Example 1

In the following example, we are going to consider the basic usage of the find_first_of() function.

#include <iostream>
#include <string>
#include <cstddef>
int main() {
   std::string str("It replaces the vowels in this sentence by asterisks.");
   std::size_t found = str.find_first_of("aeiou");
   while (found != std::string::npos) {
      str[found] = '*';
      found = str.find_first_of("aeiou", found + 1);
   }
   std::cout << str << '\n';
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

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

Example 2

In below code we have initialized string x, and finding the first occurrence position of the string = morning by using find_first_of() function.

#include<iostream>
#include<string>
using namespace std;
int main() {
   string x = "Good morning everyone!";
   cout << "String contains = " << x << endl;
   cout << "String character = " << x.find_first_of("morning");
   return 0;
}

Output

If we run the above code it will generate the following output.

String contains = Good morning everyone!
String character = 1

Example 3

In the program, we have initialized string x and assigned the position as 2. So, it starts the search from the second position in the first occurrence of the 'morning'.So here we can see that position where to start and search as it is specified.

#include<iostream>
#include<string>
using namespace std;
int main() {
   string x = "Good morning everyone!";
   cout << "String contains : " << x << '\n';
   cout << "searching from second position and finding the first occurrence of the 'morning' = " << x.find_first_of("morning", 2);
   return 0;
}

Output

Following is the output of the above code.

String contains : Good morning everyone!
searching from second position and finding the first occurrence of the 'morning' = 2                          

Example 4

In the following example we have initialized string x and finding the position of the first occurrence of a single character by using find_first_of() function.

#include<iostream>
#include<string>
using namespace std;
int main() {
   string x = "Tutorialspoint Company!";
   cout << "String = " << x << '\n';
   cout << "First occurrence position of 'u' character = " << x.find_first_of('u');
   return 0;
}  

Output

Following is the output of the above code.

String = Tutorialspoint Company!
First occurrence position of 'u' character = 1       
string.htm
Advertisements