Getting the actual length of a UTF-8 encoded std::string?

Count all first-bytes (the ones that don’t match 10xxxxxx).

int len = 0;
while (*s) len += (*s++ & 0xc0) != 0x80;

Leave a Comment