Ruby:检查东亚宽度(Unicode) [英] Ruby: Checking for East Asian Width (Unicode)

查看:292
本文介绍了Ruby:检查东亚宽度(Unicode)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Ruby,我必须以列式格式输出字符串到终端。就像这样:

  |第1行|一个字符串在这里etc 
|第2行|另一个字符串| etc

使用String#ljust和%s的拉丁文UTF8字符可以做到这一点。



但是,当字符是韩文,中文等时会出现问题。当英文的行被散列在包含韩文的行等时,列不会对齐。



如何在这里获得列对齐?有没有办法输出相当于固定宽度字体的亚洲字符?如何在Vim中显示和编辑文档?

解决方案

晚会晚了,但希望仍然有帮助:在Ruby中,您可以使用 unicode-display_width gem 来检查字符串的东亚宽度:

pre $ require'unicode / display_width'
⚀.display_width#=> 1
'一'.display_width#=> 2


Using Ruby, I have to output strings in an columnar format to the terminal. Something like this:

| row 1     | a string here     | etc
| row 2     | another string    | etc

I can do this fine with Latin UTF8 characters using String#ljust and %s.

But a problem arises when the characters are Korean, Chinese, etc. The columns simply won't align when there are rows of English interspersed with rows containing Korean, etc.

How can I get column alignment here? Is there a way to output Asian characters in the equivalent of a fixed-width font? How about for documents that are meant to be displayed and edited in Vim?

解决方案

Late to the party, but hopefully still helpful: In Ruby, you can use the unicode-display_width gem to check for a string's east-asian-width:

require 'unicode/display_width'
"⚀".display_width #=> 1
'一'.display_width #=> 2

这篇关于Ruby:检查东亚宽度(Unicode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆