std::string::find_last_not_of in C++
Last Updated :
04 Feb, 2021
It searches the string for the first character, from the end of the string, that does not match any of the characters specified in its arguments.
Return value : Index of first unmatched character when successful or string::npos if no such character found.
Syntax 1: Search for the last character that is not an element of the string str.
size_type string::find_last_not_of (const string& str) const
str : Another string with the set of characters
to be used in the search.
CPP
#include <iostream>
using namespace std;
void find_last_not_ofDemo(string str)
{
string::size_type ch = str.find_last_not_of( 'G' );
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_last_not_ofDemo(str);
return 0;
}
|
Output:
Original String : Hello World!
String to be looked in : GeeksforGeeks
First unmatched character : !
Syntax 2: Search for the last character from index idx that is not an element of the string str.
size_type string::find_last_not_of (const string& str, size_type idx) const
str : Another string with the set of characters
to be used in the search.
idx : is the index number from where we have to
start finding first unmatched character.
CPP
#include <iostream>
using namespace std;
void find_last_not_ofDemo(string str)
{
string::size_type ch = str.find_last_not_of( 'G' , 6);
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_last_not_ofDemo(str);
return 0;
}
|
Output:
Original String : geeKsforgeeks
String to be looked in : GeeksforGeeks
First unmatched character : K
Syntax 3: Searches for the last character that is or is not also an element of the C-string cstr.
size_type string::find_last_not_of (const char* cstr) const
cstr : Another C-string with the set of characters
to be used in the search.
Note that cstr may not be a null pointer (NULL).
CPP
#include <iostream>
using namespace std;
void find_last_not_ofDemo(string str)
{
string::size_type ch = str.find_last_not_of( "svmnist" , 4, 3);
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_last_not_ofDemo(str);
return 0;
}
|
Output:
Original String : GeeksforGeeks
First unmatched character : G
Syntax 4: Search for the last character from index idx that is not an element of the C-string cstr
size_type string::find_last_not_of (const string& str, size_type idx) const
cstr : Another C-string with the set of characters
to be used in the search.
idx : is the index number from where we have to
start finding first unmatched character.
Note that cstr may not be a null pointer (NULL).
Output:
Original String : GeeksforGeeks
First unmatched character : f
Syntax 5: Finds last character in str which is not equal to char c.
size_type string::find_last_not_of (const char* cstr) const
c: Character c with which contents of str are required to be compared.
CPP
#include <iostream>
using namespace std;
void find_last_not_ofDemo(string str)
{
string::size_type ch = str.find_last_not_of( 'G' );
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_last_not_ofDemo(str);
return 0;
}
|
Output:
Original String : GeeksforGeeks
First unmatched character : s
Syntax 6: Finds last character in str from index idx which is not equal to char c.
size_type string::find_last_not_of (const char* cstr, size_type idx) const
c: Character c with which contents of str are required to be compared.
idx : index from where search of the first
unmatched character is to be started
CPP
#include <iostream>
using namespace std;
void find_last_not_ofDemo(string str)
{
string::size_type ch = str.find_last_not_of( 'G' , 6);
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_last_not_ofDemo(str);
return 0;
}
|
Output:
Original String : GeeksforGeeks
First unmatched character : o
Syntax 7: Search for the last character that is also not an element of the chars_len characters of the character array chars, starting at index idx.
size_type string::find_last_not_of (const char* chars, size_type idx, size_type
chars_len) const
*chars : is the character array with which
searching of first unmatched is to be performed.
idx : index number in string
chars_len : is the character length in
*chars to be picked for searching purpose
CPP
#include <iostream>
using namespace std;
void find_last_not_ofDemo(string str)
{
string::size_type ch = str.find_last_not_of( "svmnist" , 4, 3);
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_last_not_ofDemo(str);
return 0;
}
|
Output:
Original String : GeeksforGeeks
First unmatched character : k
Similar Reads
std::string::find_first_not_of in C++
It searches the string for the first character that does not match any of the characters specified in its arguments. Here we will describe all syntaxes it holds. Return value : Index of first unmatched character when successful or string::npos if no such character found. Syntax 1: Search for the fir
6 min read
std::find_first_of in C++
std::find_first_of is used to compare elements between two containers. It compares all the elements in a range [first1,last1) with the elements in the range [first2,last2), and if any of the elements present in the second range is found in the first one , then it returns an iterator to that element.
6 min read
std::string class in C++
C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character. String vs Character ArrayString Ch
8 min read
String find() in C++
In C++, string find() is a built-in library function used to find the first occurrence of a substring in the given string. Letâs take a look at a simple example that shows the how to use this function: [GFGTABS] C++ #include <bits/stdc++.h> using namespace std; int main() { string s = "We
4 min read
std::list::sort in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::sort() sort() function is used to sort the
2 min read
std::find_if , std::find_if_not in C++
std :: find_if and std :: find_if_not are algorithm functions in C++ Standard Library in <algorithm> header file. These functions provide an efficient way to search for an element in a container using a predicate function. std :: find_if This function returns an iterator to the first element i
3 min read
std::basic_string::operator[] in C++
Returns a reference to the character at specified location pos. No bounds checking is performed. If pos > size(), the behavior is undefined. Syntax : reference operator[] (size_type pos); const_reference operator[] (size_type pos) const; Parameters : pos - position of the character to return Retu
1 min read
std::string::size() in C++
The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string. In this article, we will learn about string::size()
2 min read
<strings> library in C++ STL
Member functions String.constructor : Construct string object (public member function ).String.destructor : String destructor (public member function )String.operator= : String assignment (public member function ) Iterators Begin : Return iterator to beginning (public member function )End : Return i
3 min read
std::string::clear in C++
The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters. Parameters: none Return Value: none void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing // CPP
1 min read