Java计算控制台上要加下划线的打印字符数 [英] java calculate number of printed characters on console to underline

查看:424
本文介绍了Java计算控制台上要加下划线的打印字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,该应用程序可以打印菜单并获得一些输入信息.菜单系统具有我要强调的标题:

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

 Main Menu
 =========

标题的大小可能有所不同,所以我的第一个尝试是获取字符串长度并打印出许多指定的下划线字符.不幸的是,这在我们的日语区域中不起作用.标题存储在.properties文件中,并使用ResourceBundle类获取.

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.

我在StackOverflow中看到了一些可能的解决方案,这些解决方案似乎主要与GUI相关,所以没有帮助:

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

[更新]

仔细查看输出,似乎每个日语字符在每个英语字符中占据两个位置.是否有基于每个字符确定此功能的功能?

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?

[更新]

有什么想法吗?

西蒙(Simon)

推荐答案

终端通常使用两个插槽显示CJK字符,而不是一个,因此您必须将它们中的每一个视为两个字符.也有半角字符"占据一个插槽.获得可视字符串长度的唯一方法是循环遍历字符,将全角字符计数为两个.

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.

可以通过Unicode字符属性 EAST_ASIAN_WIDTH 来查找字符的宽度.不幸的是,标准API没有提供任何查找此属性的方法,而是

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;
}

这里是字符数据,如果您不能使用ICU4J.此数据与将字符分配给块或脚本之间可能有很多重叠;我猜想大多数HAN字符都是宽的.

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.

这篇关于Java计算控制台上要加下划线的打印字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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