C++C++11C++14C++17Introduction to C++Learn C++

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

What is wstring? How can we use wide strings? What is difference between string and wstring?

Generally in Introduction to C++ lessons, examples are started with string examples and ends with them, while the Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. In C++ there are several typedefs for common character types 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,

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

Reading wide strings from STDIN in C++

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

How to print wide strings in C++

We can use printf() function as below,

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,

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

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Get A Directory Name From A Path String

C++Introduction to C++Language FeatureLearn C++

How To Delete A File On Windows In C++ Builder

C++Introduction to C++Language FeatureLearn C++

How To Delete A Directory In C++ Builder On Windows

C++C++11C++14C++17Introduction to C++Learn C++

How to Delete, Clear, Erase A C++ Vector Member On Windows

en_USEnglish