Memos About SalesForce

Salesforceにハマってたこと!

- About Salesforce Post Blog -

SFDC APEX 全角 半角

こんにちは、管理人の@Salesforce.Zです。

再び、全角半角について

メモします。

前回、半角➡全角を共有しました。 SFDC APEX 文字列 全角変換 - Memos About SalesForce

今回のその逆を作りました。

または正規表現についても

一緒に共有したいと思います。

欲しけりゃくれてやる・・・。

探せ!

この世の全てをそこに置いてきた〜笑

目次

全角➡半角

コード

   /**
     * 半角→全角変換
     *
     * @param value 変換元文字列
     * @return 全角文字列
     */
    public static String convertHalfToFull(String value) {
        String result;
        // 変換内容がない場合
        if(String.isEmpty(value)) {
            result = '';
        }else{
            // 全角文字のみなら以降スキップ
            if (Pattern.matches('[^ -~。-゚]+', value)) {
                result = value;
            }else{
                // 濁音、半濁音は2文字で1文字に変換されるため先に実施
                Map<String, String> halftoFullMap1 = new Map<String, String>();
                halftoFullMap1.put('ガ' , 'ガ');
                halftoFullMap1.put('ギ' , 'ギ');
                halftoFullMap1.put('グ' , 'グ');
                halftoFullMap1.put('ゲ' , 'ゲ');
                halftoFullMap1.put('ゴ' , 'ゴ');
                halftoFullMap1.put('ザ' , 'ザ');
                halftoFullMap1.put('ジ' , 'ジ');
                halftoFullMap1.put('ズ' , 'ズ');
                halftoFullMap1.put('ゼ' , 'ゼ');
                halftoFullMap1.put('ゾ' , 'ゾ');
                halftoFullMap1.put('ダ' , 'ダ');
                halftoFullMap1.put('ヂ' , 'ヂ');
                halftoFullMap1.put('ヅ' , 'ヅ');
                halftoFullMap1.put('デ' , 'デ');
                halftoFullMap1.put('ド' , 'ド');
                halftoFullMap1.put('バ' , 'バ');
                halftoFullMap1.put('ビ' , 'ビ');
                halftoFullMap1.put('ブ' , 'ブ');
                halftoFullMap1.put('ベ' , 'ベ');
                halftoFullMap1.put('ボ' , 'ボ');
                halftoFullMap1.put('パ' , 'パ');
                halftoFullMap1.put('ピ' , 'ピ');
                halftoFullMap1.put('プ' , 'プ');
                halftoFullMap1.put('ペ' , 'ペ');
                halftoFullMap1.put('ポ' , 'ポ');
                halftoFullMap1.put('ヴ' , 'ヴ');

                Map<String, String> fulltoHalfMap2 = new Map<String, String>();
                fulltoHalfMap2.put('!' , '!');
                fulltoHalfMap2.put('"' , '”');
                fulltoHalfMap2.put('#' , '#');
                fulltoHalfMap2.put('$' , '$');
                fulltoHalfMap2.put('%' , '%');
                fulltoHalfMap2.put('&' , '&');
                fulltoHalfMap2.put('\'' , '’');
                fulltoHalfMap2.put('(' , '(');
                fulltoHalfMap2.put(')' , ')');
                fulltoHalfMap2.put('*' , '*');
                fulltoHalfMap2.put('+' , '+');
                fulltoHalfMap2.put('' , ',');
                fulltoHalfMap2.put('-' , '−');
                fulltoHalfMap2.put('.' , '.');
                fulltoHalfMap2.put('/' , '/');
                fulltoHalfMap2.put('0' , '0');
                fulltoHalfMap2.put('1' , '1');
                fulltoHalfMap2.put('2' , '2');
                fulltoHalfMap2.put('3' , '3');
                fulltoHalfMap2.put('4' , '4');
                fulltoHalfMap2.put('5' , '5');
                fulltoHalfMap2.put('6' , '6');
                fulltoHalfMap2.put('7' , '7');
                fulltoHalfMap2.put('8' , '8');
                fulltoHalfMap2.put('9' , '9');
                fulltoHalfMap2.put(':' , ':');
                fulltoHalfMap2.put(';' , ';');
                fulltoHalfMap2.put('<' , '<');
                fulltoHalfMap2.put('=' , '=');
                fulltoHalfMap2.put('>' , '>');
                fulltoHalfMap2.put('?' , '?');
                fulltoHalfMap2.put('@' , '@');
                fulltoHalfMap2.put('A' , 'A');
                fulltoHalfMap2.put('B' , 'B');
                fulltoHalfMap2.put('C' , 'C');
                fulltoHalfMap2.put('D' , 'D');
                fulltoHalfMap2.put('E' , 'E');
                fulltoHalfMap2.put('F' , 'F');
                fulltoHalfMap2.put('G' , 'G');
                fulltoHalfMap2.put('H' , 'H');
                fulltoHalfMap2.put('I' , 'I');
                fulltoHalfMap2.put('J' , 'J');
                fulltoHalfMap2.put('K' , 'K');
                fulltoHalfMap2.put('L' , 'L');
                fulltoHalfMap2.put('M' , 'M');
                fulltoHalfMap2.put('N' , 'N');
                fulltoHalfMap2.put('O' , 'O');
                fulltoHalfMap2.put('P' , 'P');
                fulltoHalfMap2.put('Q' , 'Q');
                fulltoHalfMap2.put('R' , 'R');
                fulltoHalfMap2.put('S' , 'S');
                fulltoHalfMap2.put('T' , 'T');
                fulltoHalfMap2.put('U' , 'U');
                fulltoHalfMap2.put('V' , 'V');
                fulltoHalfMap2.put('W' , 'W');
                fulltoHalfMap2.put('X' , 'X');
                fulltoHalfMap2.put('Y' , 'Y');
                fulltoHalfMap2.put('Z' , 'Z');
                fulltoHalfMap2.put('[' , '[');
                fulltoHalfMap2.put('\\' , '¥');
                fulltoHalfMap2.put(']' , ']');
                fulltoHalfMap2.put('^' , '^');
                fulltoHalfMap2.put('_' , '_');
                fulltoHalfMap2.put('`' , '‘');
                fulltoHalfMap2.put('a' , 'a');
                fulltoHalfMap2.put('b' , 'b');
                fulltoHalfMap2.put('c' , 'c');
                fulltoHalfMap2.put('d' , 'd');
                fulltoHalfMap2.put('e' , 'e');
                fulltoHalfMap2.put('f' , 'f');
                fulltoHalfMap2.put('g' , 'g');
                fulltoHalfMap2.put('h' , 'h');
                fulltoHalfMap2.put('i' , 'i');
                fulltoHalfMap2.put('j' , 'j');
                fulltoHalfMap2.put('k' , 'k');
                fulltoHalfMap2.put('l' , 'l');
                fulltoHalfMap2.put('m' , 'm');
                fulltoHalfMap2.put('n' , 'n');
                fulltoHalfMap2.put('o' , 'o');
                fulltoHalfMap2.put('p' , 'p');
                fulltoHalfMap2.put('q' , 'q');
                fulltoHalfMap2.put('r' , 'r');
                fulltoHalfMap2.put('s' , 's');
                fulltoHalfMap2.put('t' , 't');
                fulltoHalfMap2.put('u' , 'u');
                fulltoHalfMap2.put('v' , 'v');
                fulltoHalfMap2.put('w' , 'w');
                fulltoHalfMap2.put('x' , 'x');
                fulltoHalfMap2.put('y' , 'y');
                fulltoHalfMap2.put('z' , 'z');
                fulltoHalfMap2.put('{' , '{');
                fulltoHalfMap2.put('|' , '|');
                fulltoHalfMap2.put('}' , '}');
                fulltoHalfMap2.put('~' , '〜');
                fulltoHalfMap2.put(' ' , ' ');
                fulltoHalfMap2.put('。' , '。');
                fulltoHalfMap2.put('「' , '「');
                fulltoHalfMap2.put('」' , '」');
                fulltoHalfMap2.put('、' , '、');
                fulltoHalfMap2.put('・' , '・');
                fulltoHalfMap2.put('ヲ' , 'ヲ');
                fulltoHalfMap2.put('ァ' , 'ァ');
                fulltoHalfMap2.put('ィ' , 'ィ');
                fulltoHalfMap2.put('ゥ' , 'ゥ');
                fulltoHalfMap2.put('ェ' , 'ェ');
                fulltoHalfMap2.put('ォ' , 'ォ');
                fulltoHalfMap2.put('ャ' , 'ャ');
                fulltoHalfMap2.put('ュ' , 'ュ');
                fulltoHalfMap2.put('ョ' , 'ョ');
                fulltoHalfMap2.put('ッ' , 'ッ');
                fulltoHalfMap2.put('ー' , 'ー');
                fulltoHalfMap2.put('ア' , 'ア');
                fulltoHalfMap2.put('イ' , 'イ');
                fulltoHalfMap2.put('ウ' , 'ウ');
                fulltoHalfMap2.put('エ' , 'エ');
                fulltoHalfMap2.put('オ' , 'オ');
                fulltoHalfMap2.put('カ' , 'カ');
                fulltoHalfMap2.put('キ' , 'キ');
                fulltoHalfMap2.put('ク' , 'ク');
                fulltoHalfMap2.put('ケ' , 'ケ');
                fulltoHalfMap2.put('コ' , 'コ');
                fulltoHalfMap2.put('サ' , 'サ');
                fulltoHalfMap2.put('シ' , 'シ');
                fulltoHalfMap2.put('ス' , 'ス');
                fulltoHalfMap2.put('セ' , 'セ');
                fulltoHalfMap2.put('ソ' , 'ソ');
                fulltoHalfMap2.put('タ' , 'タ');
                fulltoHalfMap2.put('チ' , 'チ');
                fulltoHalfMap2.put('ツ' , 'ツ');
                fulltoHalfMap2.put('テ' , 'テ');
                fulltoHalfMap2.put('ト' , 'ト');
                fulltoHalfMap2.put('ナ' , 'ナ');
                fulltoHalfMap2.put('ニ' , 'ニ');
                fulltoHalfMap2.put('ヌ' , 'ヌ');
                fulltoHalfMap2.put('ネ' , 'ネ');
                fulltoHalfMap2.put('ノ' , 'ノ');
                fulltoHalfMap2.put('ハ' , 'ハ');
                fulltoHalfMap2.put('ヒ' , 'ヒ');
                fulltoHalfMap2.put('フ' , 'フ');
                fulltoHalfMap2.put('ヘ' , 'ヘ');
                fulltoHalfMap2.put('ホ' , 'ホ');
                fulltoHalfMap2.put('マ' , 'マ');
                fulltoHalfMap2.put('ミ' , 'ミ');
                fulltoHalfMap2.put('ム' , 'ム');
                fulltoHalfMap2.put('メ' , 'メ');
                fulltoHalfMap2.put('モ' , 'モ');
                fulltoHalfMap2.put('ヤ' , 'ヤ');
                fulltoHalfMap2.put('ユ' , 'ユ');
                fulltoHalfMap2.put('ヨ' , 'ヨ');
                fulltoHalfMap2.put('ラ' , 'ラ');
                fulltoHalfMap2.put('リ' , 'リ');
                fulltoHalfMap2.put('ル' , 'ル');
                fulltoHalfMap2.put('レ' , 'レ');
                fulltoHalfMap2.put('ロ' , 'ロ');
                fulltoHalfMap2.put('ワ' , 'ワ');
                fulltoHalfMap2.put('ン' , 'ン');
                fulltoHalfMap2.put('゙' , '゛');
                fulltoHalfMap2.put('゚' , '゜');
                // エスケープ必要文字
                Set<String> escapeChars = new Set<String>();
                escapeChars.add('\\');
                escapeChars.add('*');
                escapeChars.add('+');
                escapeChars.add('.');
                escapeChars.add('?');
                escapeChars.add('{');
                escapeChars.add('}');
                escapeChars.add('(');
                escapeChars.add(')');
                escapeChars.add('[');
                escapeChars.add(']');
                escapeChars.add('^');
                escapeChars.add('$');
                escapeChars.add('|');
                escapeChars.add('/');

                for (String key : halftoFullMap1.keySet()) {
                    if (value.contains(key)) {
                        value = value.replaceAll(key, halftoFullMap1.get(key));
                    }
                }

                for (String key : fulltoHalfMap2.keySet()) {
                    if (value.contains(key)) {
                        String tempKey = key;
                        if (escapeChars.contains(tempKey)) {
                            tempKey = '\\' + tempKey;
                        }
                        value = value.replaceAll(tempKey, fulltoHalfMap2.get(key));
                    }
                }
                String comma = ',';
                if(value.contains(comma)){
                    value = value.replaceAll(comma, '');
                }
                result = value;
            }
        }
        return result;
    }
使用例
    String value = '1a4|!ガ';
    UtilString.convertHalfToFull(value);

半角➡全角

コード
    /**
     * 全角→半角変換
     *
     * @param value 変換元文字列
     * @return 半角文字列
     */
    public static String convertFullToHalf(String value) {
        String result;
        // 変換内容がない場合
        if(String.isEmpty(value)) {
            result = '';
        }else{
            // 半角文字のみなら以降スキップ
            if (Pattern.matches('[ -~]+', value)) {
                result = value;
            }else{
                // 濁音、半濁音は2文字で1文字に変換されるため先に実施
                Map<String, String> fulltoHalfMap1 = new Map<String, String>();
                fulltoHalfMap1.put('ガ' , 'ガ');
                fulltoHalfMap1.put('ギ' , 'ギ');
                fulltoHalfMap1.put('グ' , 'グ');
                fulltoHalfMap1.put('ゲ' , 'ゲ');
                fulltoHalfMap1.put('ゴ' , 'ゴ');
                fulltoHalfMap1.put('ザ' , 'ザ');
                fulltoHalfMap1.put('ジ' , 'ジ');
                fulltoHalfMap1.put('ズ' , 'ズ');
                fulltoHalfMap1.put('ゼ' , 'ゼ');
                fulltoHalfMap1.put('ゾ' , 'ゾ');
                fulltoHalfMap1.put('ダ' , 'ダ');
                fulltoHalfMap1.put('ヂ' , 'ヂ');
                fulltoHalfMap1.put('ヅ' , 'ヅ');
                fulltoHalfMap1.put('デ' , 'デ');
                fulltoHalfMap1.put('ド' , 'ド');
                fulltoHalfMap1.put('バ' , 'バ');
                fulltoHalfMap1.put('ビ' , 'ビ');
                fulltoHalfMap1.put('ブ' , 'ブ');
                fulltoHalfMap1.put('ベ' , 'ベ');
                fulltoHalfMap1.put('ボ' , 'ボ');
                fulltoHalfMap1.put('パ' , 'パ');
                fulltoHalfMap1.put('ピ' , 'ピ');
                fulltoHalfMap1.put('プ' , 'プ');
                fulltoHalfMap1.put('ペ' , 'ペ');
                fulltoHalfMap1.put('ポ' , 'ポ');
                fulltoHalfMap1.put('ヴ' , 'ヴ');

                Map<String, String> fulltoHalfMap2 = new Map<String, String>();
                fulltoHalfMap2.put( '!',  '!' );
                fulltoHalfMap2.put( '”',  '"' );
                fulltoHalfMap2.put( '#',  '#' );
                fulltoHalfMap2.put( '$',  '$' );
                fulltoHalfMap2.put( '%',  '%' );
                fulltoHalfMap2.put( '&',  '&' );
                fulltoHalfMap2.put(  '’',  '\'');
                fulltoHalfMap2.put( '(',  '(' );
                fulltoHalfMap2.put( ')',  ')' );
                fulltoHalfMap2.put( '*',  '*' );
                fulltoHalfMap2.put( '+',  '+' );
                fulltoHalfMap2.put( ',',   '' );
                fulltoHalfMap2.put( '−',  '-' );
                fulltoHalfMap2.put( '.',  '.' );
                fulltoHalfMap2.put( '/',  '/' );
                fulltoHalfMap2.put( '0',  '0' );
                fulltoHalfMap2.put( '1',  '1' );
                fulltoHalfMap2.put( '2',  '2' );
                fulltoHalfMap2.put( '3',  '3' );
                fulltoHalfMap2.put( '4',  '4' );
                fulltoHalfMap2.put( '5',  '5' );
                fulltoHalfMap2.put( '6',  '6' );
                fulltoHalfMap2.put( '7',  '7' );
                fulltoHalfMap2.put( '8',  '8' );
                fulltoHalfMap2.put( '9',  '9' );
                fulltoHalfMap2.put( ':',  ':' );
                fulltoHalfMap2.put( ';',  ';' );
                fulltoHalfMap2.put( '<',  '<' );
                fulltoHalfMap2.put( '=',  '=' );
                fulltoHalfMap2.put( '>',  '>' );
                fulltoHalfMap2.put( '?',  '?' );
                fulltoHalfMap2.put( '@',  '@' );
                fulltoHalfMap2.put( 'A',  'A' );
                fulltoHalfMap2.put( 'B',  'B' );
                fulltoHalfMap2.put( 'C',  'C' );
                fulltoHalfMap2.put( 'D',  'D' );
                fulltoHalfMap2.put( 'E',  'E' );
                fulltoHalfMap2.put( 'F',  'F' );
                fulltoHalfMap2.put( 'G',  'G' );
                fulltoHalfMap2.put( 'H',  'H' );
                fulltoHalfMap2.put( 'I',  'I' );
                fulltoHalfMap2.put( 'J',  'J' );
                fulltoHalfMap2.put( 'K',  'K' );
                fulltoHalfMap2.put( 'L',  'L' );
                fulltoHalfMap2.put( 'M',  'M' );
                fulltoHalfMap2.put( 'N',  'N' );
                fulltoHalfMap2.put( 'O',  'O' );
                fulltoHalfMap2.put( 'P',  'P' );
                fulltoHalfMap2.put( 'Q',  'Q' );
                fulltoHalfMap2.put( 'R',  'R' );
                fulltoHalfMap2.put( 'S',  'S' );
                fulltoHalfMap2.put( 'T',  'T' );
                fulltoHalfMap2.put( 'U',  'U' );
                fulltoHalfMap2.put( 'V',  'V' );
                fulltoHalfMap2.put( 'W',  'W' );
                fulltoHalfMap2.put( 'X',  'X' );
                fulltoHalfMap2.put( 'Y',  'Y' );
                fulltoHalfMap2.put( 'Z',  'Z' );
                fulltoHalfMap2.put( '[',  '[' );
                fulltoHalfMap2.put(  '¥',  '\\');
                fulltoHalfMap2.put( ']',  ']' );
                fulltoHalfMap2.put( '^',  '^' );
                fulltoHalfMap2.put( '_',  '_' );
                fulltoHalfMap2.put( '‘',  '`' );
                fulltoHalfMap2.put( 'a',  'a' );
                fulltoHalfMap2.put( 'b',  'b' );
                fulltoHalfMap2.put( 'c',  'c' );
                fulltoHalfMap2.put( 'd',  'd' );
                fulltoHalfMap2.put( 'e',  'e' );
                fulltoHalfMap2.put( 'f',  'f' );
                fulltoHalfMap2.put( 'g',  'g' );
                fulltoHalfMap2.put( 'h',  'h' );
                fulltoHalfMap2.put( 'i',  'i' );
                fulltoHalfMap2.put( 'j',  'j' );
                fulltoHalfMap2.put( 'k',  'k' );
                fulltoHalfMap2.put( 'l',  'l' );
                fulltoHalfMap2.put( 'm',  'm' );
                fulltoHalfMap2.put( 'n',  'n' );
                fulltoHalfMap2.put( 'o',  'o' );
                fulltoHalfMap2.put( 'p',  'p' );
                fulltoHalfMap2.put( 'q',  'q' );
                fulltoHalfMap2.put( 'r',  'r' );
                fulltoHalfMap2.put( 's',  's' );
                fulltoHalfMap2.put( 't',  't' );
                fulltoHalfMap2.put( 'u',  'u' );
                fulltoHalfMap2.put( 'v',  'v' );
                fulltoHalfMap2.put( 'w',  'w' );
                fulltoHalfMap2.put( 'x',  'x' );
                fulltoHalfMap2.put( 'y',  'y' );
                fulltoHalfMap2.put( 'z',  'z' );
                fulltoHalfMap2.put( '{',  '{' );
                fulltoHalfMap2.put( '|',  '|' );
                fulltoHalfMap2.put( '}',  '}' );
                fulltoHalfMap2.put( '〜',  '~' );
                fulltoHalfMap2.put( ' ',  ' ' );
                fulltoHalfMap2.put( '。',  '。' );
                fulltoHalfMap2.put( '「',  '「' );
                fulltoHalfMap2.put( '」',  '」' );
                fulltoHalfMap2.put( '、',  '、' );
                fulltoHalfMap2.put( '・',  '・' );
                fulltoHalfMap2.put( 'ヲ',  'ヲ' );
                fulltoHalfMap2.put( 'ァ',  'ァ' );
                fulltoHalfMap2.put( 'ィ',  'ィ' );
                fulltoHalfMap2.put( 'ゥ',  'ゥ' );
                fulltoHalfMap2.put( 'ェ',  'ェ' );
                fulltoHalfMap2.put( 'ォ',  'ォ' );
                fulltoHalfMap2.put( 'ャ',  'ャ' );
                fulltoHalfMap2.put( 'ュ',  'ュ' );
                fulltoHalfMap2.put( 'ョ',  'ョ' );
                fulltoHalfMap2.put( 'ッ',  'ッ' );
                fulltoHalfMap2.put( 'ー',  'ー' );
                fulltoHalfMap2.put( 'ア',  'ア' );
                fulltoHalfMap2.put( 'イ',  'イ' );
                fulltoHalfMap2.put( 'ウ',  'ウ' );
                fulltoHalfMap2.put( 'エ',  'エ' );
                fulltoHalfMap2.put( 'オ',  'オ' );
                fulltoHalfMap2.put( 'カ',  'カ' );
                fulltoHalfMap2.put( 'キ',  'キ' );
                fulltoHalfMap2.put( 'ク',  'ク' );
                fulltoHalfMap2.put( 'ケ',  'ケ' );
                fulltoHalfMap2.put( 'コ',  'コ' );
                fulltoHalfMap2.put( 'サ',  'サ' );
                fulltoHalfMap2.put( 'シ',  'シ' );
                fulltoHalfMap2.put( 'ス',  'ス' );
                fulltoHalfMap2.put( 'セ',  'セ' );
                fulltoHalfMap2.put( 'ソ',  'ソ' );
                fulltoHalfMap2.put( 'タ',  'タ' );
                fulltoHalfMap2.put( 'チ',  'チ' );
                fulltoHalfMap2.put( 'ツ',  'ツ' );
                fulltoHalfMap2.put( 'テ',  'テ' );
                fulltoHalfMap2.put( 'ト',  'ト' );
                fulltoHalfMap2.put( 'ナ',  'ナ' );
                fulltoHalfMap2.put( 'ニ',  'ニ' );
                fulltoHalfMap2.put( 'ヌ',  'ヌ' );
                fulltoHalfMap2.put( 'ネ',  'ネ' );
                fulltoHalfMap2.put( 'ノ',  'ノ' );
                fulltoHalfMap2.put( 'ハ',  'ハ' );
                fulltoHalfMap2.put( 'ヒ',  'ヒ' );
                fulltoHalfMap2.put( 'フ',  'フ' );
                fulltoHalfMap2.put( 'ヘ',  'ヘ' );
                fulltoHalfMap2.put( 'ホ',  'ホ' );
                fulltoHalfMap2.put( 'マ',  'マ' );
                fulltoHalfMap2.put( 'ミ',  'ミ' );
                fulltoHalfMap2.put( 'ム',  'ム' );
                fulltoHalfMap2.put( 'メ',  'メ' );
                fulltoHalfMap2.put( 'モ',  'モ' );
                fulltoHalfMap2.put( 'ヤ',  'ヤ' );
                fulltoHalfMap2.put( 'ユ',  'ユ' );
                fulltoHalfMap2.put( 'ヨ',  'ヨ' );
                fulltoHalfMap2.put( 'ラ',  'ラ' );
                fulltoHalfMap2.put( 'リ',  'リ' );
                fulltoHalfMap2.put( 'ル',  'ル' );
                fulltoHalfMap2.put( 'レ',  'レ' );
                fulltoHalfMap2.put( 'ロ',  'ロ' );
                fulltoHalfMap2.put( 'ワ',  'ワ' );
                fulltoHalfMap2.put( 'ン',  'ン' );
                fulltoHalfMap2.put( '゛',  '゙' );
                fulltoHalfMap2.put( '゜',  '゚' );
                // エスケープ必要文字
                Set<String> escapeChars = new Set<String>();
                escapeChars.add('\\');
                escapeChars.add('*');
                escapeChars.add('+');
                escapeChars.add('.');
                escapeChars.add('?');
                escapeChars.add('{');
                escapeChars.add('}');
                escapeChars.add('(');
                escapeChars.add(')');
                escapeChars.add('[');
                escapeChars.add(']');
                escapeChars.add('^');
                escapeChars.add('$');
                escapeChars.add('|');
                escapeChars.add('/');

                for (String key : fulltoHalfMap1.keySet()) {
                    if (value.contains(key)) {
                        value = value.replaceAll(key, fulltoHalfMap1.get(key));
                    }
                }

                for (String key : fulltoHalfMap2.keySet()) {
                    if (value.contains(key)) {
                        String tempKey = key;
                        if (escapeChars.contains(tempKey)) {
                            tempKey = '\\' + tempKey;
                        }
                        value = value.replaceAll(tempKey, fulltoHalfMap2.get(key));
                    }
                }
                String comma = ',';
                if(value.contains(comma)){
                    value = value.replaceAll(comma, '');
                }
                result = value;
            }
        }
        return result;
    }
使用例
    String value = '4Aa|';
    UtilString.convertHalfToFull(value);

正規表現

推薦サイト

初心者: 初心者の正規表現 - Qiita

SFDC: https://help.salesforce.com/articleView?id=000213868&language=ja&type=1

Salesforceでも正規表現 | セールスフォース知恵袋

その他の全角半角変換

http://vaindespair.blogspot.com/2012/03/blog-post_6992.htmlvaindespair.blogspot.com

http://www.jp-cloudia.co.jp/techblog/?p=618

正規表現に使えそうな置換

Sublimeの操作

テキストエディター「sublime text」で、ある文章を「.」/「,」ごとに改行するようにReplaceする方法は以下の通り

[Find] -> [Replace]で置換ウィンドウを開き、一番右上の「.*」をチェック。

「.」はそのままでは正規表現の「何でもいい文字1つ」という意味になってしまうためエスケープ文字「\」を「.」の前に入力しておく。

「\」の入力方法は、[Option]キー+「¥」ボタン

f:id:jude2016:20180815150201p:plain