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

Learn to Use Strings (string) in C++ – All You Need To Know

In computer programming alphanumeric characters (texts) are displayed with string types. Strings are objects with an array of bytes that represent sequences of characters.

As an introduction to C++, in addition to int, float, double there is another data type we should know. 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

In C++, In CLANG standards, Borland C++, Dev C++, GNU C++, Visual C++, Strings (string) are the string class for the single-byte characters represented with string class, this string class provides objects with an interface similar to a standard container of bytes, with some features specifically designed to operate well with strings, composed with single-byte characters. In other term, an object is defined with a string class, composed of 8bit chars (bytes). C++ IDE uses Unicode-based strings; this standard is for GUI forms to support all languages to provide applications globally.

How to define a string in C++

Simply we can define a string as below,

This is how to print strings in C++

When we print out strings, we can use std:: cout command. Here is a very simple example to use wide strings,

to avoid using std in every std command you should use this namespace as below,

How to read a string from STDIN in C++

We can get strings with std:: cin command as given example below,

This is how to print strings in C++ with printf()

In C++, we can use printf() function to print strings as below,

How to find the length or size of a string in C++

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

What are the different types of strings available in C++?

In Modern C++, array of chars, 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 to understand Unicode Strings in C++.

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

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

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

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

How To Learn The Move Constructors In Modern C++?