英文:
Is there an equivalent to the wcwidth() function in Go?
问题
POSIX函数wcwidth()
用于计算在终端上打印时给定wchar_t
的宽度。例如,wcwidth(L'A')
返回1
,wcwidth(L'字')
返回2
,等等。还有一个函数wcswidth()
用于计算整个字符串的宽度,如果存在组合重音符号,则这个函数非常有用。
Go标准库或附加库中是否存在类似的函数?如果不存在,是否有一种简单的方法来创建一个足够相似的函数?
英文:
The POSIX function wcwidth()
computes the width of a given wchar_t
when printed on a terminal. For instance, wcwidth(L'A')
returns 1
, wcwidth(L'字')
returns 2
, etc. There is also a function wcswidth()
which computes the width of an entire string—this is useful if combining accents are present.
Does a similar function exist in the Go standard library or the supplementary libraries? If not, is there an easy way to make something sufficiently similar?
答案1
得分: 8
在Go标准库或附加库中是否存在类似的功能?
我认为最流行的库是go-runewidth。
示例代码:
package main
import (
"github.com/mattn/go-runewidth"
)
func main() {
println(runewidth.StringWidth("A")) // 输出 1
println(runewidth.StringWidth("字")) // 输出 2
}
希望对你有帮助!
英文:
> Does a similar function exist in the Go standard library or the supplementary libraries?
I believe the most popular library for this is go-runewidth.
Example:
package main
import (
"github.com/mattn/go-runewidth"
)
func main() {
println(runewidth.StringWidth("A")) // prints 1
println(runewidth.StringWidth("字")) // prints 2
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论