C++Builder The Right Way To Access Character Elements Of A Wide String

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
The Right Way To Access Character Elements Of A Wide String
By Yilmaz Yoru July 29, 2021

How can I access a character of a wstring? 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) uses wchar_t 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,
C++:
std::string 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 add characters with + or += operators ; we can use push_back() and insert() methods to add chars to a wide string. Let’s see how we add and remove characters from wide strings.

How to Access to a Character of a Wide String in C++​

Wide Strings are a class contains arrays of characters with useful methods, and we can access, or modify their characters easily. In Modern C++, while string contents are defined between L” and ” with L literal, characters are defined between L’ and ‘ with L literal. We can access to these wide characters with [ ] brackets; we can use at(), front() and back() methods to add chars to a string. Let’s see how we access and modify characters of strings.

Access to a Character of a Wide String with [ ] brackets​

This example below shows how we can access to characters of a wide string by using [ ] brackets,
C++:
#include <iostream>
#include <string>
 
int main()
{
 std::wstring ws = L"ABCD";
 
 wchar_t wc;
 
 wc = ws[0];     // set a char to first char of wide string
 std::wcout << (wchar_t)(wc) << std::endl;  // printing a character
 std::wcout << ws[1] << std::endl;  // printing character of a wide string with [ ]
 
 getchar();
 return 0;
}
and the wide string outputs will be,
Код:
A
B
as you see in this example we print the first and second characters of the wide string.

Access to a Character of a Wide String with at() Method​

We can access any character of a string by using at() method. This example below shows how we can access to characters of a string by using at() method,
C++:
#include <iostream>
#include <string>
 
int main()
{
 std::wstring ws = L"ABCD";
 
 std::wcout << ws.at(0) << std::endl; // printing character of a string
 
 std::wcout << ws << std::endl;  // printing a string
 
 ws.at(0) = (wchar_t)(88); // set first char to L'X'
 std::wcout << ws << std::endl;
 
 ws.at(1) = L'-';        // set second char to '-'
 std::wcout << ws << std::endl;
 
 ws.at(3) = ws.at(2);  // set forth char to third one
 std::wcout << ws << std::endl;
 
 getchar();
 return 0;
}
and the wide string outputs will be,
Код:
A
ABCD
XBCD
X-CD
X-CC
as you see in this example we print the first and second characters of the wide string.

Access to a Character of a Wide String with front() and back() Methods​

We can easily reach to the first and the last char of a wide string by using front() and back() methods. This example below shows how we can access to characters of a wide string by using front() and back() methods,
C++:
#include <iostream>
#include <string>
 
int main()
{
 std::wstring ws = L"ABCD";
 
 std::cout << ws.front() << std::endl;
 
 std::cout << ws.back() << std::endl;
 
 getchar();
 return 0;
}
and the string outputs will be,
Код:
A
D
As you see we can easily reach to the first front and the last back char of a string. Sometimes it is important to check first or last char, if it is a number, if it is a question mark, etc.

Note that, these methods above can be used with std::string, std::u16string, std::u32string too.