(…OK, a language lawyer would nitpick suggesting “How many string types…”, but I wanted a catchier title.)
So, if you program in Python and you see something enclosed by either single quotes or double quotes, you have a string:
s = 'Connie'
Something similar happens in Java, with string literals like “Connie” implemented as instances of the java.lang.String class:
String s = "Connie";
Now, let’s enter – drumroll, please – The Realm of C++! And the fun begins 😊
So, let’s consider this simple line of C++ code:
auto s1 = "Connie";
What is the type of s1?
std::string? A char array? (Hey, “Connie” is six characters, but don’t forget the terminating NUL!)
So, you can use your favorite IDE, and hover over the variable name, and get the deduced type. Visual Studio C++ IntelliSense suggests it’s “const char*”. Wow!
And what about “Connie”s?
auto s2 = "Connie"s;
No, it’s not the plural of “Connie”. And it’s not a malformed Saxon genitive either. This time s2 is of type std::string! Thank you operator””s introduced in C++14!
But, are we done? Of course, not! Don’t forget: It’s C++! 😊
For example, you can have u8”Connie”, which represents a UTF-8 literal. And, of course, we need a thread on StackOverflow to figure out “How are u8-literals supposed to work?”
And don’t forget L”Connie”, u”Connie” and U”Connie”, which represent const wchar_t*, const char16_t* (UTF-16 encoded) and const char32_t* (UTF-32 encoded) respectively.
Now we are done, right? Not yet!
In fact, you can combine the previous prefixes with the standard s-suffix, for example: L”Connie”s is a std::wstring! U”Connie”s is a std::u32string. And so on.
Done, right? Not yet!! In fact, there are raw string literals to consider, too. For example: R”(C:\Path\To\Connie)”, which is a const char* to “C:\Path\To\Connie” (well, this saves you escaping \ with \\).
And don’t forget the combinations of raw string literals with the above prefixes and optionally the standard s-suffix, as well: LR”(C:\Path\To\Connie)”, UR”(C:\Path\To\Connie)”, LR”(C:\Path\To\Connie)”s, UR”(C:\Path\To\Connie)”s, and more!
Oh, and in addition to the standard std::string class, and other standard std::basic_string-based typedefs (e.g. std::wstring, std::u16string, std::u32string, etc.), there are platform/library specific string classes, like ATL/MFC’s CString, CStringA and CStringW. And Qt brings QString to the table. And wxWidgets does the same with its wxString.
Wow! And I would not be surprised if I missed some other string variation out 😊
P.S. With all this string variety (maybe too much…), what about adding to the C++ Standard Library some convenient functions for at least common string operations like trimming spaces and converting strings to upper case and lower case? All in all, C++ does already have rocket-science stuff like Bessel functions in its Standard Library. While, back in the old MFC days, CString already offered convenient methods like Trim, MakeLower and MakeUpper, just to name a few.
If you want to learn modern C++ from scratch, in a fun and interesting way, with engaging slides and demo code, please check out my course!