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 another term, an object defined with string class, composed with 8bit chars (bytes).

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

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++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

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

How to Create a New Windows FMX Static Library In C++

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

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

en_USEnglish