How can I access a character of a wstring in my C++ app? How can I use the at() method of wide strings? Can I use front() and back() methods in std::wstring to access characters?
Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. Wide Strings (std::wstring) use 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++ software, 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?
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"; |
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 access to wide characters of a wstring with Iterators; we can use begin(), end() methods to define range .
Wide Strings (wstring) are a class contains arrays of wide characters with useful methods, and we can access, or modify their characters easily. In C++, while wide string contents are defined between L” and ” with literal L, wide characters are defined between L’ and ‘ with literal L
Learn to Use find() Method of wstring in your C++ app
The find() method is a Wide String Method that finds a wide string in its wide string . There can be a position to start as a second parameter, search begins from this position
to the end of wide string. find() method returns the position of the first character of the wide substring. If no such substring is found in that wide string then it returns npos of the wstring (std::wstring::npos).
Syntax:
1 2 3 |
size_type find( const basic_string& str, size_type pos = 0 ) const; |
How to search a Wide String with the find() Method in C++
Simply we can find a wide string in a string by using its find method as given example below.
1 2 3 4 5 6 7 |
std::wstring wstr = L"This is a Wide String"; int result = wstr.find( L"String" ); // Searching "String" in string str if( result == std::wstring::npos ) std::wcout << L"Wide String not found\n"; else std::wcout << L"Wide string Found\n"; |
This full C++ example below shows how we can find a string in another string by using find() method,
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.find( L"String" ); // Searching "String" in string str if( pos != std::wstring::npos ) { std::wcout << L"Wide String found 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 at 5 |
as you see in this example we found the first “String” that is at the position 5 which means starts from the 6th character ‘S’ of this string.
How to search a wide string from a given position in a C++ app
The find() method is a Wide String Method that finds a string in its string . There can be a position to start as a second parameter, search begins from this position
to the end of wide string. find() method returns the position of the first character of the found substring. If no such substring is found in that wide string then it returns npos of the wstring (std::wstring::npos).
Syntax:
1 2 3 |
size_type find( const basic_string& str, size_type pos = 0 ) const; |
This full C++ example below shows how we can find a string in another string by using find() method,
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.find( L"String", 6 ); // Searching a string from position 6 in string wstr if( pos != std::wstring::npos ) { std::wcout << L"Wide String found 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 wide string and prints the position if it finds the wide string as below,
1 2 3 4 |
This String is a Wide String Wide String found at 22 |
as you see in this example we found the second “String” text in a wide string by using search start position at 6. Result is at the position 22 which means starts from the 23rd character ‘S’ of this string.