java中计算输出字符个数,Java计算控制台上打印字符的数量以下划线

I have a console app which prints a menu and get some input etc. The menu system has titles I underline:

Main Menu

=========

The title can be of varying size so my first attempt was to take the string length and print that many of the designated underline character. Unfortunately this doesn't work in our Japanese locale. The titles are stored in .properties files and fetched using the ResourceBundle class.

I saw some possible solutions in StackOverflow which seem mainly related to GUIs so have not helped:

public static int getGraphemeCount(String text) {

int graphemeCount = 0;

BreakIterator graphemeCounter = BreakIterator.getCharacterInstance();

graphemeCounter.setText(text);

while (graphemeCounter.next() != BreakIterator.DONE)

graphemeCount++;

return graphemeCount;

}

public static void outputTitle(String title,char underChar) {

String underline = repeats(underChar,getGraphemeCount(title));

System.out.printf("\n\t%s\n\t%s\n",title,underline);

}

There's an extra wrinkle in that not all the text will be translated (e.g. company or product names).

[update]

Having had a closer look at the output it appears the individual Japanese characters take up two places for each english character. Is there a function to determine this on a per character basis?

[update]

Any thoughts?

Simon

解决方案

Terminals typically show CJK characters using two slots instead of just one, so you have to count each of them as two characters. There are also "half width characters" that occupy a single slot. The only way to get the visual string length is looping over the characters, counting full width characters as two.

The width of a character can be looked up as the Unicode character property EAST_ASIAN_WIDTH. Unfortunately the standard API does not provide any method for looking up this property, but the ICU4J library does:

char c = ...;

int width;

switch (UCharacter.getIntPropertyValue(c, UProperty.EAST_ASIAN_WIDTH)) {

case UCharacter.EastAsianWidth.WIDE:

case UCharacter.EastAsianWidth.FULLWIDTH:

width = 2; break;

default:

width = 1;

}

Here's the character data if you can't use ICU4J. There's probably a lot of overlap between this data and the assignment of characters to blocks or scripts; I would guess most HAN characters are wide for example.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值