Java 全角字符转半角字符(icu的Transliterator使用)

Java 全角字符转半角字符 互相转换。

可以直接实现,也可以直接用jar包

有ICU包直接用就好

<dependency>
    <groupId>com.ibm.icu</groupId>
    <artifactId>icu4j</artifactId>
    <version>69.1</version>
</dependency>

代码示例如下: 

public static void main(String[] args) {
    String half = "ABC一二三";
    String full = "ABC一二三";
    String jHalf = "コンニチハ";
    String jFull = "コンニチハ";
    // 半角->全角
    Transliterator literator1 = Transliterator.getInstance("Halfwidth-Fullwidth");
    System.out.println(literator1.transliterate(half));
    System.out.println(literator1.transliterate(full));
    System.out.println(literator1.transliterate(jHalf));
    System.out.println(literator1.transliterate(jFull));

    System.out.println();

    // 全角->半角    
    Transliterator literator2 = Transliterator.getInstance("Fullwidth-Halfwidth");
    System.out.println(literator2.transliterate(half));
    System.out.println(literator2.transliterate(full));
    System.out.println(literator2.transliterate(jHalf));
    System.out.println(literator2.transliterate(jFull));

}

 

相关推荐
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页