Site icon Learn C++

Learn To Use Wide Strings (wstring) In C++

In this post, you’ll discover what wstring is. How can we use long strings? What is the distinction between string and wstring? By learning wide strings (wstring) and terms, it will help you to build C++ applications with the use of a C++ IDE.

Generally, in Introduction to C++ lessons, examples start with string examples and end with them, while Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. In C++ there are several typedefs for common character types that are provided: String types are defined in header <string>.

Here are the string types defined in std namespace with their char type and C++ standard

String TypeChar TypeDefinitionC++ Standard
std::stringcharstd::basic_string<char>C++
std::wstringwchar_tstd::basic_string<wchar_t>C++
std::u8stringchar8_tstd::basic_string<char8_t>C++20
std::u16string char16_tstd::basic_string<char16_t>C++11
std::u32string char32_tstd::basic_string<char32_t>C++11

Here are the string types defined in std::pmr namespace with their char type and C++ standard

String TypeChar TypeDefinitionC++ Standard
std::pmr::stringcharstd::pmr::basic_string<char>C++17
std::pmr::wstring wchar_tstd::pmr::basic_string<wchar_t>C++17
std::pmr::u8stringchar8_tstd::pmr::basic_string<char8_t>C++20
std::pmr::u16string char16_tstd::pmr::basic_string<char16_t>C++17
std::pmr::u32stringchar32_tstd::pmr::basic_string<char32_t>C++17

What you need to know about wide strings in C++

Wide strings are the string class for wide characters represented with wstring and alphanumeric characters are stored and displayed in string forms. In another terms wstring stores for the alphanumeric text with 2 or 4 byte chars. Wide strings are the instantiation of the basic_string class template that uses wchar_t as the character type. Simply we can define a wstring as below,

[crayon-65f975d890762895086325/]

When we print out wide strings we must use wcout command. Here is a very simple example to use wide strings,

[crayon-65f975d890769484030032/]

Reading wide strings from STDIN in C++

We can get wstrings with wcin command as given example below,

[crayon-65f975d89076b895971218/]

How to print wide strings in C++

We can use printf() function as below,

[crayon-65f975d89076d127898660/]

This is the way to get the length, size and other properties of a C++ wide string

We can use length(), size(), max_size() methods of wstring class to get length, size and max_size() of wide string. See example below,

[crayon-65f975d89076f299210867/]

What else should I know about wide strings in C++?

In Modern C++ , strings and wide strings can be used, we highly recommend you to use unicode strings. Unicode standard for UnicodeString provides a unique number for every character (8, 16 or 32 bits) more than ASCII (8 bits) characters. UnicodeStrings are being used widely because of support to languages world wide and emojis. In modern C++ nowadays there are two types of strings used; array of chars (char strings) and UnicodeStrings (WideStrings and AnsiStrings are older, not compatible with all features now). CLANG / C++ Builder / GNU C / VC++ compilers, IDEs are using this standard for GUI forms to support all languages to provided applications in global. More information about the structure of Unicode Strings can be found here . RAD Studio , Delphi & C++ Builder uses Unicode-based strings: that is, the type String is a Unicode string (System.UnicodeString) instead of an ANSI string. If you want to transform your codes to Unicode strings we recommend you this article.




Please check this post about Unicode Strings in C++ On Windows

Exit mobile version