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

What Is The rfind() Search Method in Modern C++ Software?

How can I find a string in a string in the reverse direction – in other words, by starting to search at the end of the string and working backwards to the start of it? What kind of methods can I use in my C++ software to find a string in a std::string? How can I use rfind() method 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 a string in with rfind() method in reverse search direction.

rfind() Method – how to search strings in reverse with the rfind() method in C++

The rfind() method is a String Method that finds a string in its string starting from a reverse direction. There can be a reverse position to start as a second parameter, search begins from this position to the end of string. find() method returns the position of the first character of the found substring. If no such substring is found in that string then it returns the npos of the string (std::string::npos).

Syntax:

Then, we can find a string in a string by using its rfind() method as given example below.

Output of this code will print the original string and prints the position if it finds the string as below,

as you see in this example we found the second “String” that is at the position 17 while find method finds the first String at the position 5. This is a good example how rfind() works in a reverse string search.

rfind() Method – how to search a string in reverse from a given position of the string

The rfind() method is a String Method that finds a string in its string starting from a reverse position, search begins from this position to the front of string. find() method returns the position of the first character of the found substring. If no such substring is found in that string then it returns the npos of the string (std::string::npos).

Syntax:

Output of this code will print the original string and prints the position if it finds the string as below,

As you see we started to search from position 6 and we find the first “String” at position 5

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
CC++C++17Introduction to C++Language FeatureLearn C++

What Is Count In C Programming?

CC++Introduction to C++Language FeatureLearn C++

Why Is C The Most Popular Programming Language?

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

How To Use Switch In C++ And C Programming?

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

What Are Header Files In C++ and C Programming?