C++C++11C++14C++17Introduction to C++Learn C++

How To find_first_not_of() Or find_last_not_of() In C++ Apps

How To findfirstnotof Or findlastnotof In C++ Apps

How can I find a first or last character that is not a character that i am searching in a string? What kind of methods I can use in my C++ apps to find the first character equal to NONE of the characters in the given string in a std::string? How can I use find_first_not_of(), find_last_not_of() methods with strings?

Modern C++ uses Strings, Wide Strings, and Unicode Strings to support worldwide languages. Strings (std::string) uses char as the character type which means they are ASCII chars and they are an instantiation of the basic_string class template. In C++, there are several typedefs for common character types and one of them is std::string types that are defined in header <string>.

strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In other words, string stores the alphanumeric text with 1-byte chars called char. Strings are the instantiation of the basic_string class template that uses char as the character type. In modern C++, simply we can define a string as below,

Strings (string) are a class that contains arrays of characters with useful methods, and we can access, or modify their characters easily. In C++, while string contents are defined between ” and “, characters are defined between ‘ and ‘.

The string has methods to append, assign, copy, align, replace or operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can find the first or last character equal to none of the characters in the given string by using find_first_not_of() and find_last_not_of() methods.

How to search a string with the find_first_not_of() method in C++?

The find_first_not_of() Method is a String Method that searches given character in this method and finds the first character position from the string that is not a given character. In another term, the find_first_not_of() method finds the first character equal to none of the characters in the given string or character sequence. If the character is not present method returns the npos of the string (std::string::npos).

Simply we can find the first character equal to none of the characters in the given string by using its find_first_not_of() method as given syntax.

What is the syntax of the find_first_not_of() C++ method?

Here we can find a character of a string in another string by using its find_first_not_of() method as given full example below.

Output of this code will be as below,

as you see we find the first character after the ‘-‘ characters in our string

How can I search a string with the find_last_not_of() method in C++?

The find_last_not_of() Method is a String Method that search given character in this method and finds the last character position from string that is not a given character. In another term, find_last_not_of() method finds the last character equal to none of the characters in the given string or character sequence. If the character is not present method returns the npos of the string (std::string::npos).

Simply we can find the last character equal to none of the characters in the given string by using its find_last_not_of() method as given syntax.

Syntax:

Here we can find a character of a string in another string by using its find_last_not_of() method as given full example below.

The output of this code will be as below,

as you see we find the last character before the last ‘-‘ characters in our string

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++

How To Use The All-Powerful swscanf function In Your C++ App

C++ComponentsLanguage FeatureLearn C++

How To Use A Sharpen Effect in C+ Software On Windows?

C++C++11C++14C++17Introduction to C++Learn C++

What Is The Erase WString Method In A Modern C++ App

C++C++17Introduction to C++Learn C++

This Is How Trivial Copy Constructors Work In A C++ App