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

How To Use find_first_of() And find_last_of() In C++ Apps

How can I search only a single character in a string in my C++ apps? What kind of methods I can use to find a character in a std::string? How can I use find_first_of(), find_last_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 terms, 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 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 characters in a string by using find_first_of() and find_last_of() methods.

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

The find_first_of() Method is a String Method that finds the first character of a string (a character sequence) in another string. In this method it searches only for a single character in a string. If the character is not present in the string it returns the npos of the string (std::string::npos).

Simply we can find a character of a string in another string by using its find_first_of() method as given syntax.

What is the syntax of find_first_of()?:

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

The output of this code will be as below,

As you see here, we find the first ‘r’ character in our string at the position 3

How do I search a string with the find_last_of() method in C++?

The find_last_of() Method is a String Method that finds the last character of a string (a character sequence) in another string. In this method it search only a single character while the search is a string. If the character is not present in a string it, returns the npos of the string (std::string::npos).

Simply we can find a character of a string in another string by using its find_last_of() method as given syntax.

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

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

The output of this code will be as below,

as you see we found the last ‘r’ character which is in “.org” part at the position 16.

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, and Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Generic ProgrammingLearn C++Templates

What Is A Template In C++?

C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

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

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++