Created
May 30, 2017 01:21
-
-
Save jmahmood/16f48a5fde0eae8b95864268be530782 to your computer and use it in GitHub Desktop.
Converting Strings to Zenkaku (全角) or Hankaku (半角) using Salesforce APEX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ZenkakuHankaku{ | |
/* The character list retrieved from a JavaScript library, can't find now though. :/ */ | |
public static String toZenkaku(String s) { | |
if (String.isBlank(s)){ | |
return s; | |
} | |
String str = s.replace('\u0020', '\u3000').replace('\u0022', '\u201D').replace('\u0027', '\u2019').replace('\u00A5', '\uFFE5').replace('\uff61', '\u3002').replace('\uff62', '\u300c').replace('\uff63', '\u300d').replace('\uff64', '\u3001').replace('\uff65', '\u30fb').replace('\uff66', '\u30f2').replace('\uff67', '\u30a1').replace('\uff68', '\u30a3').replace('\uff69', '\u30a5').replace('\uff6a', '\u30a7').replace('\uff6b', '\u30a9').replace('\uff6c', '\u30e3').replace('\uff6d', '\u30e5').replace('\uff6e', '\u30e7').replace('\uff6f', '\u30c3').replace('\uff70', '\u30fc').replace('\uff71', '\u30a2').replace('\uff72', '\u30a4').replace('\uff73', '\u30a6').replace('\uff74', '\u30a8').replace('\uff75', '\u30aa').replace('\uff76', '\u30ab').replace('\uff77', '\u30ad').replace('\uff78', '\u30af').replace('\uff79', '\u30b1').replace('\uff7a', '\u30b3').replace('\uff7b', '\u30b5').replace('\uff7c', '\u30b7').replace('\uff7d', '\u30b9').replace('\uff7e', '\u30bb').replace('\uff7f', '\u30bd').replace('\uff80', '\u30bf').replace('\uff81', '\u30c1').replace('\uff82', '\u30c4').replace('\uff83', '\u30c6').replace('\uff84', '\u30c8').replace('\uff85', '\u30ca').replace('\uff86', '\u30cb').replace('\uff87', '\u30cc').replace('\uff88', '\u30cd').replace('\uff89', '\u30ce').replace('\uff8a', '\u30cf').replace('\uff8b', '\u30d2').replace('\uff8c', '\u30d5').replace('\uff8d', '\u30d8').replace('\uff8e', '\u30db').replace('\uff8f', '\u30de').replace('\uff90', '\u30df').replace('\uff91', '\u30e0').replace('\uff92', '\u30e1').replace('\uff93', '\u30e2').replace('\uff94', '\u30e4').replace('\uff95', '\u30e6').replace('\uff96', '\u30e8').replace('\uff97', '\u30e9').replace('\uff98', '\u30ea').replace('\uff99', '\u30eb').replace('\uff9a', '\u30ec').replace('\uff9b', '\u30ed').replace('\uff9c', '\u30ef').replace('\uff9d', '\u30f3').replace('\uff9e', '\u3099').replace('\uff9f', '\u309a'); | |
List<Integer> chars = new List<Integer>(); | |
for (Integer sint: str.getChars()){ | |
chars.add(sint >= 32 && sint <= 127 ? sint + 65248 : sint); // ascii characters | |
} | |
return String.fromCharArray(chars); | |
} | |
public static String toHankaku(String s) { | |
if (String.isBlank(s)){ | |
return s; | |
} | |
String str = s.replace('\u3000', '\u0020').replace('\u201D', '\u0022').replace('\u2019', '\u0027').replace('\uFFE5', '\u00A5').replace('\u3002', '\uff61').replace('\u300c', '\uff62').replace('\u300d', '\uff63').replace('\u3001', '\uff64').replace('\u30fb', '\uff65').replace('\u30f2', '\uff66').replace('\u30a1', '\uff67').replace('\u30a3', '\uff68').replace('\u30a5', '\uff69').replace('\u30a7', '\uff6a').replace('\u30a9', '\uff6b').replace('\u30e3', '\uff6c').replace('\u30e5', '\uff6d').replace('\u30e7', '\uff6e').replace('\u30c3', '\uff6f').replace('\u30fc', '\uff70').replace('\u30a2', '\uff71').replace('\u30a4', '\uff72').replace('\u30a6', '\uff73').replace('\u30a8', '\uff74').replace('\u30aa', '\uff75').replace('\u30ab', '\uff76').replace('\u30ad', '\uff77').replace('\u30af', '\uff78').replace('\u30b1', '\uff79').replace('\u30b3', '\uff7a').replace('\u30b5', '\uff7b').replace('\u30b7', '\uff7c').replace('\u30b9', '\uff7d').replace('\u30bb', '\uff7e').replace('\u30bd', '\uff7f').replace('\u30bf', '\uff80').replace('\u30c1', '\uff81').replace('\u30c4', '\uff82').replace('\u30c6', '\uff83').replace('\u30c8', '\uff84').replace('\u30ca', '\uff85').replace('\u30cb', '\uff86').replace('\u30cc', '\uff87').replace('\u30cd', '\uff88').replace('\u30ce', '\uff89').replace('\u30cf', '\uff8a').replace('\u30d2', '\uff8b').replace('\u30d5', '\uff8c').replace('\u30d8', '\uff8d').replace('\u30db', '\uff8e').replace('\u30de', '\uff8f').replace('\u30df', '\uff90').replace('\u30e0', '\uff91').replace('\u30e1', '\uff92').replace('\u30e2', '\uff93').replace('\u30e4', '\uff94').replace('\u30e6', '\uff95').replace('\u30e8', '\uff96').replace('\u30e9', '\uff97').replace('\u30ea', '\uff98').replace('\u30eb', '\uff99').replace('\u30ec', '\uff9a').replace('\u30ed', '\uff9b').replace('\u30ef', '\uff9c').replace('\u30f3', '\uff9d').replace('\u3099', '\uff9e').replace('\u309a', '\uff9f'); | |
List<Integer> chars = new List<Integer>(); | |
for (Integer sint: str.getChars()){ | |
chars.add(sint >= 65248+32 && sint <= 65248+127 ? sint - 65248 : sint); // ascii characters | |
} | |
return String.fromCharArray(chars); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment