
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ String::find_last_not_of() function
The C++ std::string::find_last_not_of() function is used to find the position of the last character in a string that does not match any character in a specified set. It searches the string from the end towards the beginning and returns the position of the last character that doesn't match any character in the provided set.
If all the characters in the string are part of the set, it returns std::string::npos.
Syntax
Following is the syntax for std::string::find_last_not_of() function.
size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept; or size_t find_last_not_of (const char* s, size_t pos = npos) const; or size_t find_last_not_of (const char* s, size_t pos, size_t n) const; or size_t find_last_not_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 to 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 that does not match.
Example 1
Following is an example to finding the last charcater not in another string using std::string::find_last_not_of in C++.
#include <iostream> #include <string> using namespace std; int main() { string str = " Tutorialspoint10years@ "; size_t found = str.find_last_not_of("abcdefghijklmnopqrstuvwxyz "); if (found != string::npos) { cout << "The last non-alphabetic character is " << str[found]; cout << " at index " << found << '\n'; } return 0; }
Output
Let us compile and run the above program, this will produce the following result −
The last non-alphabetic character is @ at index 22
Example 2
Following is an another example to find the last non-digit in a string.
#include <iostream> #include <string> using namespace std; int main() { string str = "12345abcXYZ6789"; size_t pos = str.find_last_not_of("0123456789"); if (pos != string::npos) { cout << " '" << str[pos] << "' is at index " << pos << endl; } return 0; }
Output
If we run the above code it will generate the following output.
'Z' is at index 10
Example 3
In this program, we are skipping whitespace at the beginning of the string.
#include <iostream> #include <string> using namespace std; int main() { string str = " Tutorialspoint10years@"; size_t pos = str.find_last_not_of(" \t\n"); if (pos != string::npos) { cout << "last non-whitespace character is '" << str[pos] << "' at index " << pos << endl; } return 0; }
Output
Following is the output of the above code.
Last non-whitespace character is '@' at index 25
Example 4
In this program we have initialized two strings to find the last character not in another string.
#include <iostream> #include <string> using namespace std; int main() { string str1 = "Tutorialspoint10years@"; string str2 = "10years@"; size_t pos = str1.find_last_not_of(str2); if (pos != string::npos) { cout << "last character in '" << str1 << "' not in '" << str2 << "' is '" << str1[pos] << "' at index " << pos << endl; } return 0; }
Output
Following is the output of the above code.
last character in 'Tutorialspoint10years@' not in '10years@' is 't' at index 13