使用串字符细胞的数目 [英] Number of character cells used by string
问题描述
我有一个程序,输出使用UTF-8字符串文本表,我需要测量所使用的一个字符串,所以我可以正确对齐等宽字符细胞的数量。如果可能的话,我想和标准功能做到这一点。
I have a program that outputs a textual table using UTF-8 strings, and I need to measure the number of monospaced character cells used by a string so I can align it properly. If possible, I'd like to do this with standard functions.
推荐答案
从的 UTF-8和统一code常见问题的Unix / Linux :
字符数可以在C使用被计算在一个便携的方式 mbstowcs(NULL,S,0)
。像其他任何支持的编码,只要相应的语言环境已选择本工程为UTF-8。硬连线技术,计算一个UTF-8字符串中的字符数是计算所有字节,除了那些在0x80的范围 - 为0xBF,因为这些都只是延续字节,而不是他们自己的角色。不过,需要数字符出奇很少出现在应用程序中。
The number of characters can be counted in C in a portable way using
mbstowcs(NULL,s,0)
. This works for UTF-8 like for any other supported encoding, as long as the appropriate locale has been selected. A hard-wired technique to count the number of characters in a UTF-8 string is to count all bytes except those in the range 0x80 – 0xBF, because these are just continuation bytes and not characters of their own. However, the need to count characters arises surprisingly rarely in applications.
这篇关于使用串字符细胞的数目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!