C++ String::find_last_of() function



The C++ std::string::find_last_of() function is used to locate the last occurrence of any character from a mentioned set within a string. It searches the string from the end towards the beginning, returning the index of the last character found that matches the any character in the set. If no character from the set is found, it returns std::string::npos.

Syntax

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

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

Parameters

  • str − It indicates the another string object.
  • pos − It indicates the position of the last 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 last character.

Example 1

Following is an example to find the std::string::find_last_of using C++.

#include<iostream>
using namespace std;
int main() {
   string str = "Tutorialspoint";
   cout << "String:" << str << '\n';
   cout << "last occurances = " << str.find_last_of("poi");
   return 0;
}  

Output

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

String:Tutorialspoint
last occurances = 11

Example 2

Following is an another example finding the last occurance of any character in a string with specified position and length.

#include <iostream>
#include <string>
using namespace std;
int main() {
   string str = "Tutorialspoint";
   string chars = "al";
   size_t pos = str.find_last_of(chars, 10);
   if (pos != string::npos) {
      cout << "Last occurrence is at index " << pos << endl;
   } else {
      cout << "not found in string" << endl;
   }
   return 0;
}

Output

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

Last occurrence is at index = 7

Example 3

In this example, we are assigning a empty string and a string character. So, as it is a empty string it cannot search or find the last character in its arguments.

#include <iostream>
#include <string>
using namespace std;
int main() {
   string str = "";
   string chars = "al";
   size_t pos = str.find_last_of(chars);
   if (pos != string::npos) {
      cout << "Last occurrence '" << chars << "' is at index " << pos << endl;
   } else {
      cout << " Empty String " << endl;
   }
   return 0;
}

Output

Following is the output of the above code.

Empty String                     

Example 4

In this program we have initialized a string and assigned a character which is not present in the string.

#include <iostream>
#include <string>
using namespace std;
int main() {
   string str = "hello world";
   string chars = "xyz";
   size_t pos = str.find_last_of(chars);
   if (pos != string::npos) {
      cout << "Last occurrence '" << chars << "' is at index " << pos << endl;
   } else {
      cout << "Character not found in string (unmatched character)" << endl;
   }
   return 0;
} 

Output

Following is the output of the above code.

Character not found in string (unmatched character)   
string.htm
Advertisements