I have a string in which there can be characters of both English and Russian alphabets, and it is not quite clear to me how to count the number of characters in such a string.

string test = "ТестTest"; cout << test.length(); 

As you know, in the output stream we get not the number of characters, but the number of bytes, that is, not 8, but 12. And I would like to somehow find out exactly the number of characters. What is the correct solution for this situation?

    1 answer 1

    The easiest option is to use wider strings to fit the character into one element. Example:

     #include <string> #include <iostream> int main() { std::wstring test = L"ТестTest"; std::wcout << test.length(); } 

    Conclusion :

     8 

    A better option is to use the appropriate external libraries to support Unicode , for example, ICU .