How to know the number of characters in utf8 strin

2019-06-28 05:58发布

问题:

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:

  1. converting UTF8 string to wchar_t string
  2. use wcslen function and get result

But I need more simpler and crossplatform solution.

Thanks in advance.

回答1:

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.



回答2:

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-bit 1..10... (with the number of 1s on the left 2 or more) followed by successive bytes of the form 10... (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 form 10... - i.e. counting only the first bytes in all UTF-8 characters.



回答3:

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:

size_t i, cnt;
for (cnt=i=0; s[i]; i++) if (s[i]<0x80 || s[i]>0xbf) cnt++;

Note that s must point to an array of unsigned char in order for the comparisons to work.