i want to know is there a simple way to determine the number of characters in UTF8
string.
For example, in windows it can be done by:
- converting
UTF8
string towchar_t
string - use
wcslen
function and get result
But I need more simpler and crossplatform solution.
Thanks in advance.
If the string is known to be valid UTF-8, simply take the length of the string in bytes, excluding bytes whose values are in the range 0x80-0xbf:
Note that
s
must point to an array ofunsigned char
in order for the comparisons to work.The entire concept of a "number of characters" does not really apply to Unicode, as codes do not map 1:1 to glyphs. The method proposed by @borrible is fine if you want to establish storage requirements in uncompressed form, but that is all that it can tell you.
For example, there are code points like the "zero width space", which do not take up space on the screen when rendered, but occupy a code point, or modifiers for diacritics or vowels. So any statistic would have to be specific to the concrete application.
A proper Unicode renderer will have a function that can tell you how many pixels will be used for rendering a string if that information is what you're after.
UTF-8 characters are either single bytes where the left-most-bit is a
0
or multiple bytes where the first byte has left-most-bit1..10...
(with the number of 1s on the left 2 or more) followed by successive bytes of the form10...
(i.e. a single 1 on the left). Assuming that your string is well-formed you can loop over all the bytes and increment your "character count" every time you see a byte that is not of the form10...
- i.e. counting only the first bytes in all UTF-8 characters.