A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_string_find_last_not_of.htm below:

C++ String::find_last_not_of() function

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 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  

string.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4