How can I find a first or last character that is not a character that i am searching in a string in my C++ app? What kind of methods I can use 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 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>.
Table of Contents
What is a wstring in a C++ app?
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 that 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 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 wide string with the find_first_not_of() method in a C++ app
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 wide 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 wide string or character sequence. If the character is not present method returns the npos of the wstring (std::string::npos).
Simply we can find the first character equal to none of the characters in the given wstring by using its find_first_not_of() method as given syntax.
Syntax:
1 2 3 4 5 |
size_type find_first_not_of( const basic_string& str) const noexcept; size_type find_first_not_of( const basic_string& str, size_type pos = 0 ) const noexcept; |
A full C++ app example of using the find_first_not_of() method to search a wstring
Here we can find a character of a wstring in another wstring by using its find_first_not_of() method as given full 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 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"-----LearnCPlusPlus.org-----"; int pos = wstr.find_first_not_of(L"-"); // find the first character from a wstring which is not '-' if( pos != std::wstring::npos ) { std::wcout << L"The First Character found at " << pos << std::endl; } else { std::wcout << L"The First Charcater not found" << std::endl; } getchar(); return 0; } |
Output of this code will be as below,
1 2 3 |
The First Character found at 5 |
as you see we find the first character after the ‘-‘ characters in our wstring
How to search a wide 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 wide 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 wide string or character sequence. If the character is not present method returns the npos of the wstring (std::string::npos).
Simply we can find the last character equal to none of the characters in the given wstring by using its find_last_not_of() method as given syntax.
Syntax:
1 2 3 4 5 |
size_type find_last_not_of( const basic_string& str) const noexcept; size_type find_last_not_of( const basic_string& str, size_type position) const noexcept; |
A full example of using the find_last_not_of() method to search a wstring in a C++ app
Here we can find a character of a wstring in another wstring by using its find_last_not_of() method as given full 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 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"-----LearnCPlusPlus.org-----"; int pos = wstr.find_last_not_of(L"-"); // find the last character from a wstring which is not '-' if( pos != std::wstring::npos ) { std::wcout << L"The Last Character found at " << pos << std::endl; } else { std::wcout << L"The Last Charcater not found" << std::endl; } getchar(); return 0; } |
Output of this code will be as below,
1 2 3 |
The Last Character found at 22 |
as you see we find the last character before the last ‘-‘ characters in our wstring
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition