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

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>.

Table of Contents

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::u16stringchar16_tstd::basic_string<char16_t>C++11
std::u32stringchar32_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::wstringwchar_tstd::pmr::basic_string<wchar_t>C++17
std::pmr::u8stringchar8_tstd::pmr::basic_string<char8_t>C++20
std::pmr::u16stringchar16_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_stringclass template that useswchar_tas the character type. Simply we can define a wstring as below,

1

2

3

std::wstring wstr = L"This is a Wide String\n";

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

1

2

3

4

5

6

7

8

9

10

11

12

13

#include <iostream>

#include <string>

int main()

{

std::wstring wstr = L"This is a Wide String\n";

std::wcout << wstr;

getchar();

return 0;

}

Reading wide strings from STDIN in C++

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#include <iostream>

#include <string>

int main()

{

std::wstring wstr;

std::wcout << L"Enter a wide string:";

std::wcin>> wstr;

std::wcout << L"Your wide string is: '" << wstr << L"'\n";

// Let's wait a key press

char ch;

while (std::cin.readsome(&ch, 1) != 0) ;

getchar();

return 0;

}

How to print wide strings in C++

We can use printf() function as below,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#include <iostream>

#include <string>

int main()

{

std::wstring wstr="This is My Wide String";

wprintf(L"My Wide String is %s\n", wstr.w_str());

getchar();

return 0;

}

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,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

#include <iostream>

#include <string>

int main()

{

std::wstring wstr = L"This is Wide String\n";

std::wcout << L"Length of your wide string:" << wstr.length()<< '\n';

std::wcout << L"Size of your wide string:" << wstr.size()<< '\n';

std::wcout << L"Max Size of your wide string:" << wstr.max_size()<< '\n';

char ch;

while (std::cin.readsome(&ch, 1) != 0) ;

getchar();

return 0;

}

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

Learn To Use Wide Strings (wstring) In C++ (2024)
Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5846

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.