C++ › Strings & string_view

How do you handle Unicode and encodings in C++ strings?

Advanced

Answer

std::string holds bytes (often UTF-8) with no inherent encoding awareness—size() returns bytes, not characters. C++ has limited built-in Unicode support: char8_t/std::u8string (C++20) mark UTF-8, char16_t/char32_t for UTF-16/32, but proper Unicode processing (normalization, grapheme clusters) usually requires libraries like ICU. Treating std::string as UTF-8 bytes is a common pragmatic approach.
std::u8string s = u8"héllo";  // UTF-8 literal (char8_t)
Real-world example A text app stores UTF-8 in std::string but uses ICU for correct case-folding and normalization.

Common follow-ups: Does size() count characters or bytes? | When do you need a Unicode library?