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>.
Table of Contents
Here are the string types defined in std namespace with their char type and C++ standard.
String Type | Char Type | Definition | C++ Standard |
std::string | char | std::basic_string<char> | C++ |
std::wstring | wchar_t | std::basic_string<wchar_t> | C++ |
std::u8string | char8_t | std::basic_string<char8_t> | C++20 |
std::u16string | char16_t | std::basic_string<char16_t> | C++11 |
std::u32string | char32_t | std::basic_string<char32_t> | C++11 |
Here are the string types defined in std::pmr namespace with their char type and C++ standard.
String Type | Char Type | Definition | C++ Standard |
std::pmr::string | char | std::pmr::basic_string<char> | C++17 |
std::pmr::wstring | wchar_t | std::pmr::basic_string<wchar_t> | C++17 |
std::pmr::u8string | char8_t | std::pmr::basic_string<char8_t> | C++20 |
std::pmr::u16string | char16_t | std::pmr::basic_string<char16_t> | C++17 |
std::pmr::u32string | char32_t | std::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,
1 2 3 |
std::string str = "This is a String\n"; |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> int main() { std::string str = "This is a String\n"; std::cout << str; getchar(); return 0; } |
to avoid using std in every std command you should use this namespace as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> using namespace std; int main() { string str = "This is a String\n"; cout << str; getchar(); return 0; } |
How to read a string from STDIN in C++
We can get strings with std:: cin command as given example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> int main() { std::string str; std::cout << "Enter a string:"; std::cin >> str; std::cout << "Your string is: '" << str << "'\n"; // Let's wait for key press char ch; while (std::cin.readsome(&ch, 1) != 0) ; getchar(); return 0; } |
This is how to print strings in C++ with printf()
In C++, we can use printf() function to print strings as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { std::string str ="This is My String"; printf("My String is '%s'", str.c_str()); getchar(); return 0; } |
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,
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::string str = "This is a String\n"; std::cout << "My string is: '" << str << "'\n"; std::cout << "Length of your string:" << str.length() << '\n'; std::cout << "Size of your string:" << str.size() << '\n'; std::cout << "Max size of string:" << str.max_size() << '\n'; return 0; } |
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++.