c++ - How to get the accurate length of a std::string? -
i trimming long std::string
fit in text container using code.
std::string appdelegate::gettrimmedstringwithrange(std::string text, int range) { if (text.length() > range) { std::string str(text,0,range-3); return str.append("..."); } return text; }
but in case of other languages hindi "हिन्दी"
length of std::string
wrong.
my question how can retrieve accurate length of std::string in test cases.
thanks
assuming you're using utf-8, can convert string simple (hah!) unicode , count characters. grabbed example rosettacode.
#include <iostream> #include <codecvt> int main() { std::string utf8 = "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"; // u+007a, u+00df, u+6c34, u+1d10b std::cout << "byte length: " << utf8.size() << '\n'; std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv; std::cout << "character length: " << conv.from_bytes(utf8).size() << '\n'; }
Comments
Post a Comment