问题
i am not too familiar with java (coming from c++/c#). I have a need for to use java in order to convert some documents from halfwidth japanese charaters to full width.
would someone be kind and provide some example to start with. i have not had luck to finding some sample.
just a note it has to be in Java.
回答1:
Found this on Japanese blogger Arai's site:
String data1 = "全角ひらがな"; // full-width hiragana
String data2 = "全角カタカナ"; // full-width katakana
String data3 = "半角カタカナ"; // half-width katakana
Transliterator transliterator = Transliterator.getInstance("Hiragana-Katakana");
System.out.println(transliterator.transliterate(data1));
System.out.println(transliterator.transliterate(data2));
System.out.println(transliterator.transliterate(data3));
This should result in all 3 Japanese strings being transliterated as you would expect. Give this a shot a let me know if it doesn't work.
回答2:
Have you seen the ICU userguide and ICU APIdoc on the topic? You can use the Transliterator demo to check the behavior, such as "Halfwidth-Fullwidth"
回答3:
I've created a small, simple library to handle all types of kana conversion in Java, details here
To convert from half-width to full-width, just download the JAR and then write the code like this:
import mariten.kanatools.KanaConverter;
// rest of code...
String half_width = "半角カタカナ";
int conv_flags = KanaConverter.OP_HAN_KATA_TO_ZEN_KATA;
String full_width = KanaConverter.convertKana(half_width, conv_flags);
System.out.println(full_width)
//半角カタカナ
来源:https://stackoverflow.com/questions/6524208/how-to-convert-japanese-half-full-width-characters-using-icu-library