public member function

std::string::find_last_of

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

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

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

Parameters

str
string containing the characters to search for in the object. The first character of the string that compares equal to any of the characters in str is considered a match.
s
Array with a sequence of characters. The last character of 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 character to search for is only determined by parameter n.
In the third version, a null-terminated sequence (c-string) is expected, and the amount 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 last character in the string to be taken into consideration for possible matches. The default value npos indicates that the entire string is considered.

Return Value

The position of the last 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
22
23
24
// string::find_last_of
#include <iostream>
#include <string>
using namespace std;

void SplitFilename (const string& str)
{
  size_t found;
  cout << "Splitting: " << str << endl;
  found=str.find_last_of("/\\");
  cout << " folder: " << str.substr(0,found) << endl;
  cout << " file: " << str.substr(found+1) << endl;
}

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

  SplitFilename (str1);
  SplitFilename (str2);

  return 0;
}


Splitting: /usr/bin/man
 folder: /usr/bin
 file: man
Splitting: c:\windows\winhelp.exe
 folder: c:\windows
 file: winhelp.exe

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_last_of ( const basic_string& str,
                           size_type pos = npos ) const;
size_type find_last_of ( const charT* s, size_type pos,
                           size_type n ) const;
size_type find_last_of ( const charT* s,
                           size_type pos = npos ) const;
size_type find_last_of ( charT c, size_type pos = npos ) const;


See also