gpt4 book ai didi

c - 字符串使用的字符单元格数

转载 作者:IT王子 更新时间:2023-10-29 00:14:27 26 4
gpt4 key购买 nike

我有一个使用 UTF-8 字符串输出文本表的程序,我需要测量字符串使用的等宽字符单元格的数量,以便正确对齐它。如果可能的话,我想用标准函数来做到这一点。

最佳答案

来自 UTF-8 and Unicode FAQ for Unix/Linux :

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.

关于c - 字符串使用的字符单元格数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5117393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com