std::string::find_first_not_of in C++
Last Updated :
11 Jul, 2017
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 first character that is not an element of the string str.
size_type string::find_first_not_of (const string& str) const
str : Another string with the set of characters
to be used in the search.
#include <iostream>
using namespace std;
void find_first_not_ofDemo(string str1, string str2)
{
string::size_type ch = str1.find_first_not_of(str2);
cout << "First unmatched character : " ;
cout << str1[ch];
}
int main()
{
string str1( "Hello World!" );
string str2( "GeeksforGeeks" );
cout << "Original String : " << str1 << endl;
cout << "String to be looked in : " << str2 << endl;
find_first_not_ofDemo(str1, str2);
return 0;
}
|
Output:
Original String : Hello World!
String to be looked in : GeeksforGeeks
First unmatched character : H
Syntax 2: Search for the first character from index idx that is not an element of the string str.
size_type string::find_first_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.
#include <iostream>
using namespace std;
void find_first_not_ofDemo(string str1, string str2)
{
string::size_type ch = str1.find_first_not_of(str2, 3);
cout << "First unmatched character : " ;
cout << str1[ch];
}
int main()
{
string str1( "geeKsforgeeks" );
string str2( "GeeksforGeeks" );
cout << "Original String : " << str1 << endl;
cout << "String to be looked in : " << str2 << endl;
find_first_not_ofDemo(str1, str2);
return 0;
}
|
Output:
Original String : geeKsforgeeks
String to be looked in : GeeksforGeeks
First unmatched character : K
Syntax 3: Searches for the first character that is or is not also an element of the C-string cstr.
size_type string::find_first_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).
#include <iostream>
using namespace std;
void find_first_not_ofDemo(string str)
{
string::size_type ch = str.find_first_not_of( "geeksforgeeks" );
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_first_not_ofDemo(str);
return 0;
}
|
Output:
Original String : GeeksforGeeks
First unmatched character : G
Syntax 4: Search for the first character from index idx that is not an element of the C-string cstr
size_type string:: find_first_not_of (const char* cstr, size_type idx) const
cstr : 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.
Note that cstr may not be a null pointer (NULL).
#include <iostream>
using namespace std;
void find_first_not_ofDemo(string str)
{
string::size_type ch = str.find_first_not_of( "geeksForgeeks" , 5);
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_first_not_ofDemo(str);
return 0;
}
|
Output:
Original String : GeeksforGeeks
First unmatched character : f
Syntax 5: Finds first character in str which is not equal to char c.
size_type string::find_first_not_of (char c) const
c Character c with which contents of str are required to be compared.
#include <iostream>
using namespace std;
void find_first_not_ofDemo(string str)
{
string::size_type ch = str.find_first_not_of( 'G' );
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_first_not_ofDemo(str);
return 0;
}
|
Output:
Original String : GeeksforGeeks
First unmatched character : e
Syntax 6: Finds first character in str from index idx which is not equal to char c.
size_type string::find_first_not_of (char c, 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
#include <iostream>
using namespace std;
void find_first_not_ofDemo(string str)
{
string::size_type ch = str.find_first_not_of( 'G' , 6);
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_first_not_ofDemo(str);
return 0;
}
|
Output:
Original String : GeeksforGeeks
First unmatched character : o
Syntax 7: Search for the first 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_first_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
#include <iostream>
using namespace std;
void find_first_not_ofDemo(string str)
{
string::size_type ch = str.find_first_not_of( "svmnist" , 4, 3);
cout << "First unmatched character : " ;
cout << str[ch];
}
int main()
{
string str( "GeeksforGeeks" );
cout << "Original String : " << str << endl;
find_first_not_ofDemo(str);
return 0;
}
|
Output:
Original String : GeeksforGeeks
First unmatched character : f
If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...