How can I find a string in a string in reverse direction? What kind of methods I can use in C++ software to find a string in a std::string? How can I use rfind() method with strings?
Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. Wide Strings (std::wstring) uses wcha_tr as the character type which means they are 2-bytes chars and they are an instantiation of the basic_string class template. In C++, there are several type definitions for common character types and one of them is std::wstring types that are defined in header <string>.
wstrings are the string class for 2-bytes characters represented with wstring and alphanumeric characters are stored and displayed in wide string forms. In other terms, wstring stores for the alphanumeric text with 2-byte chars, called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. In modern C++, simply we can define a wide string with L” ” literal as below,
1 2 3 |
std::wstring wstr = L"This is a Wide String"; |
Wide Strings (wstring) are a class contains arrays of wide characters with useful methods, and we can access, or modify their characters easily. Note that, In C++, while wide string contents are defined between L” and “ with literal L, wide characters are defined between L’ and ‘ with literal L
The wstring has methods to append, assign, copy, align, replace or operate with other wide strings. These methods can be used in all string methods with their appropriate syntax. We can find a wide string in another wide string in reverse direction with rfind() method..
How can I reverse-search a wide string with rfind() Method in C++ software?
The rfind() method is a String Method that finds a wide string in its wide 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 wstring. find() method returns the position of the first character of the found substring. If no such substring is found in that wstring then it returns the npos of the wstring (std::wstring::npos).
Syntax:
1 2 3 |
size_type rfind( const basic_string& str) const; |
Then, we can find a wstring in a wstring by using its rfind() method as given example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"This String is a Wide String"; std::wcout << wstr << std::endl; int pos = wstr.rfind( L"String"); // Searching a wstring from reverse if( pos != std::wstring::npos ) { std::wcout << L"Wide String found reverse at " << pos << std::endl; } else { std::wcout << L"Wide String not found" << std::endl; } getchar(); return 0; } |
Output of this code will print the original wstring and prints the position if it finds the wstring as below,
1 2 3 4 |
This String is a Wide String Wide String found reverse at 22 |
as you see in this example we found the second “String” that is at the position 22 while find method finds the first ‘String’ at the position 5. This is a good example how rfind() works in a reverse string search.
How to use the rfind() method to search a wide string in reverse from a given position?
The rfind() method is a String Method that finds a wide string in its wide string starting from a reverse position. The 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 wstring then it returns the npos of the wstring (std::wstring::npos).
What is the syntax of rfind()?
1 2 3 |
size_type rfind( const basic_string& str, size_type position) const; |
We can find a wstring in a wstring reverse from a given position by using its rfind() method as given example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"This String is a Wide String"; std::wcout << wstr << std::endl; int pos = wstr.rfind( L"String", 6); // Searching a wstring from reverse before the position 6 if( pos != std::wstring::npos ) { std::wcout << L"Wide String found reverse at " << pos << std::endl; } else { std::wcout << L"Wide String not found" << std::endl; } getchar(); return 0; } |
Output of this code will print the original string and prints the position if it finds the string as below,
1 2 3 4 |
This String is a Wide String Wide String found reverse at 5 |
As you see we reversibly started to search from position6 and we find the first “String” at the position 5