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