简体   繁体   中英

How to convert Japanese half/full width characters using ICU Library

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.

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.

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"

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)
//半角カタカナ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM