Full-width → half-width conversion with Java String (full-width kana → half-width kana)

public class Zen2HanUtil {
    private static final Map<Character, String> zen2HanMap = new HashMap<>();

    // https://so-zou.jp/web-app/text/fullwidth-halfwidth/
    private static final String ZEN_CHARS = "0 1 2 3 4 5 6 7 8 9 "
            + "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
            + "a b c d e f g h i j k l m n o p q r s t u v w x y z "
            + "Ai Ue Okakiku Kekosa Sashisuseso What is it?"
            + "Aiueokakikukekosashisusesotachitsutetonaninune nohahifuhehomamimumemoyayuyorarirurerowon"
            + "ゔ Giguge Gozajizuze Zodajizu de Dobabibu Bebo Papi Puppe Poa Iu ぉ ぉ"
            + "Vuga giggegozajizu zezo da zu de dobababi bu bebo papipeppo wa yeo yo"
            + "-"゜ ,. ・ """
            + "! ” # $ % & ’ ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ "
            + "⦅ ⦆ ¢ £ ¬  ̄ ¦ ¥ ₩ │ ← ↑ → ↓ ■ ○";
    private static final String HAN_CHARS = "0 1 2 3 4 5 6 7 8 9 "
            + "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
            + "a b c d e f g h i j k l m n o p q r s t u v w x y z "
            + "I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry."
            + "I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry."
            + "Vuga Guge Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go"
            + "Vuga Guge Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go"
            + "ー ゚, ‥ ・ """
            + "! \" # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \\ ] ^ _ ` { | } ~ "
            + "⦅ ⦆ ¢ £ ¬ ¯ ¦ ¥ ₩ │ ← ↑ → ↓ ■ ○";
    static {
        String[] zenSplit = ZEN_CHARS.split(" ");
        String[] hanSplit = HAN_CHARS.split(" ");
        if (zenSplit.length != hanSplit.length) {
            throw new RuntimeException("char count not match. zen=" + zenSplit.length + ", han=" + hanSplit.length);
        }
        for (int i = 0; i < zenSplit.length; i++) {
            zen2HanMap.put(zenSplit[i].charAt(0), hanSplit[i]);
        }
    }

    public static String conv(String s) {
        StringBuilder sb = new StringBuilder();
        s.chars().mapToObj(c -> (char) c).forEach(c -> {
            if (c == ' ') {
                sb.append(' ');
            } else {
                String han = zen2HanMap.get(c);
                if (han == null) {
                    sb.append(c);
                } else {
                    sb.append(han);
                }
            }
        });
        return sb.toString();
    }
}

Recommended Posts

Full-width → half-width conversion with Java String (full-width kana → half-width kana)
[Java] Full-width ⇔ half-width conversion
Endian conversion with JAVA
[Java] Align characters even with mixed half-width and full-width characters
About full-width ⇔ half-width conversion of character strings in Java
Java8 list conversion with Stream map
Java string
Split a string with ". (Dot)" in Java
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
Type conversion from java BigDecimal type to String type
Java type conversion
Read a string in a PDF file with Java
[Java] String padding
Java string processing
Java type conversion (String, int, Date, Calendar, etc.)
Split string (Java)
Install java with Homebrew
Change seats with java
Install Java with Ansible
URL to String conversion
[Java] String comparison and && and ||
Uri → String, String → Uri type conversion
Java string multiple replacement
Comfortable download with JAVA
Switch java with direnv
[Note] Java: String search
[Note] Java: String survey
About Java String class
Download Java with Ansible
Let's scrape with Java! !!
Leet string conversion program
Build Java with Wercker
[Java] Date type conversion
[Java] Get Charset with Apathce Tika / Initialize String from Charset [Kotlin]
Initialization with an empty string to an instance of Java String type