Number of character cells used by string

前端 未结 6 743
离开以前
离开以前 2020-12-02 14:56

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

6条回答
  •  自闭症患者
    2020-12-02 15:10

    From 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.

提交回复
热议问题