C++ String Library - find_last_of


Advertisements

Description

It searches the string for the last character that matches any of the characters specified in its arguments.

Declaration

Following is the declaration for std::string::find_last_of.

size_t find_last_of (const string& str, size_t pos = npos) const;

C++11

size_t find_last_of (const string& str, size_t pos = npos) const noexcept;

C++14

size_t find_last_of (const string& str, size_t pos = npos) const noexcept;

Parameters

  • str − It is a string object.

  • len − It is used to copy the characters.

  • pos − Position of the first character to be copied.

Return Value

none

Exceptions

if an exception is thrown, there are no changes in the string.

Example

In below example for std::string::find_last_of.

#include <iostream>
#include <string>
#include <cstddef>
void SplitFilename (const std::string& str) {
   std::cout << "Splitting: " << str << '\n';
   std::size_t found = str.find_last_of("/\\");
   std::cout << " path: " << str.substr(0,found) << '\n';
   std::cout << " file: " << str.substr(found+1) << '\n';
}

int main () {
   std::string str1 ("/usr/bin/man");
   std::string str2 ("c:\\windows\\winhelp.exe");

   SplitFilename (str1);
   SplitFilename (str2);

   return 0;
}
string.htm
Advertisements