Why does std::string have a length() member function in addition to size()?

As per the documentation, these are just synonyms. size() is there to be consistent with other STL containers (like vector, map, etc.) and length() is to be consistent with most peoples’ intuitive notion of character strings. People usually talk about a word, sentence or paragraph’s length, not its size, so length() is there to make … Read more

How to convert an enum to a string in modern C++

Magic Enum header-only library provides static reflection for enums (to string, from string, iteration) for C++17. #include <magic_enum.hpp> enum Color { RED = 2, BLUE = 4, GREEN = 8 }; Color color = Color::RED; auto color_name = magic_enum::enum_name(color); // color_name -> “RED” std::string color_name{“GREEN”}; auto color = magic_enum::enum_cast<Color>(color_name) if (color.has_value()) { // color.value() -> … Read more

Changing a character in a string

Don’t modify strings. Work with them as lists; turn them into strings only when needed. >>> s = list(“Hello zorld”) >>> s [‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘z’, ‘o’, ‘r’, ‘l’, ‘d’] >>> s[6] = ‘W’ >>> s [‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’] >>> “”.join(s) ‘Hello … Read more

How do I replace a character at a specific index in JavaScript?

In JavaScript, strings are immutable, which means the best you can do is to create a new string with the changed content and assign the variable to point to it. You’ll need to define the replaceAt() function yourself: String.prototype.replaceAt = function(index, replacement) { return this.substring(0, index) + replacement + this.substring(index + replacement.length); } And use … Read more

Are the strings in argv modifiable?

From the C11 standard draft N1570, §5.1.2.2.1/2: The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination. They are modifiable. That means they are not string literals. But be careful: the upper citation only … Read more

how std::u8string will be different from std::string?

Since the difference between u8string and string is that one is templated on char8_t and the other on char, the real question is what is the difference between using char8_t-based strings vs. char-based strings. It really comes down to this: type-based encoding. Any char-based string (char*, char[], string, etc) may be encoded in UTF-8. But … Read more

What is the original type of interpolated string?

The new interpolated string syntax is part compiler magic and part runtime classes. Let’s go through all the scenarios and see what is actually happening. var s = $”{DateTime.Now}”; This gets compiled as this: string s = string.Format(“{0}”, DateTime.Now); See Try Roslyn for details. string s = $”{DateTime.Now}”; This gets compiled as this: string s … Read more

Is it reasonable to use std::basic_string as a contiguous buffer when targeting C++03?

I’d consider it quite safe to assume that std::string allocates its storage contiguously. At the present time, all known implementations of std::string allocate space contiguously. Moreover, the current draft of C++ 0x (N3000) [Edit: Warning, direct link to large PDF] requires that the space be allocated contiguously (§21.4.1/5): The char-like objects in a basic_string object … Read more