Joh*_*ann 36 java string android diacritics
在Android中是否有任何方法(据我所知)没有java.text.Normalizer,从String中删除任何重音.例如"éàù"变成"eau".
如果可能的话,我想避免解析String来检查每个字符!
Gui*_*ume 86
java.text.Normalizer
是否在Android中(无论如何最新版本).你可以使用它.
编辑供参考,以下是如何使用Normalizer
:
string = Normalizer.normalize(string, Normalizer.Form.NFD);
string = string.replaceAll("[^\\p{ASCII}]", "");
Run Code Online (Sandbox Code Playgroud)
(粘贴在下面评论中的链接)
这可能不是最有效的解决方案,但它可以解决问题,并且适用于所有 Android 版本:
\n\nprivate static Map<Character, Character> MAP_NORM;\nstatic { // Greek characters normalization\n MAP_NORM = new HashMap<Character, Character>();\n MAP_NORM.put('\xce\xac', '\xce\xb1');\n MAP_NORM.put('\xce\xad', '\xce\xb5');\n MAP_NORM.put('\xce\xaf', '\xce\xb9');\n MAP_NORM.put('\xcf\x8c', '\xce\xbf');\n MAP_NORM.put('\xcf\x8d', '\xcf\x85');\n MAP_NORM.put('\xce\xae', '\xce\xb7');\n MAP_NORM.put('\xcf\x82', '\xcf\x83');\n MAP_NORM.put('\xcf\x8e', '\xcf\x89');\n MAP_NORM.put('\xce\x86', '\xce\xb1');\n MAP_NORM.put('\xce\x88', '\xce\xb5');\n MAP_NORM.put('\xce\x8a', '\xce\xb9');\n MAP_NORM.put('\xce\x8c', '\xce\xbf');\n MAP_NORM.put('\xce\x8e', '\xcf\x85');\n MAP_NORM.put('\xce\x89', '\xce\xb7');\n MAP_NORM.put('\xce\x8f', '\xcf\x89');\n}\n\npublic static String removeAccents(String s) {\n if (s == null) {\n return null;\n }\n StringBuilder sb = new StringBuilder(s);\n\n for(int i = 0; i < s.length(); i++) {\n Character c = MAP_NORM.get(sb.charAt(i));\n if(c != null) {\n sb.setCharAt(i, c.charValue());\n }\n }\n\n return sb.toString();\n}\n
Run Code Online (Sandbox Code Playgroud)\n
小智 5
我已经根据我的需要调整了 Rabi 的解决方案,我希望它可以帮助某人:
private static Map<Character, Character> MAP_NORM;
public static String removeAccents(String value)
{
if (MAP_NORM == null || MAP_NORM.size() == 0)
{
MAP_NORM = new HashMap<Character, Character>();
MAP_NORM.put('À', 'A');
MAP_NORM.put('Á', 'A');
MAP_NORM.put('Â', 'A');
MAP_NORM.put('Ã', 'A');
MAP_NORM.put('Ä', 'A');
MAP_NORM.put('È', 'E');
MAP_NORM.put('É', 'E');
MAP_NORM.put('Ê', 'E');
MAP_NORM.put('Ë', 'E');
MAP_NORM.put('Í', 'I');
MAP_NORM.put('Ì', 'I');
MAP_NORM.put('Î', 'I');
MAP_NORM.put('Ï', 'I');
MAP_NORM.put('Ù', 'U');
MAP_NORM.put('Ú', 'U');
MAP_NORM.put('Û', 'U');
MAP_NORM.put('Ü', 'U');
MAP_NORM.put('Ò', 'O');
MAP_NORM.put('Ó', 'O');
MAP_NORM.put('Ô', 'O');
MAP_NORM.put('Õ', 'O');
MAP_NORM.put('Ö', 'O');
MAP_NORM.put('Ñ', 'N');
MAP_NORM.put('Ç', 'C');
MAP_NORM.put('ª', 'A');
MAP_NORM.put('º', 'O');
MAP_NORM.put('§', 'S');
MAP_NORM.put('³', '3');
MAP_NORM.put('²', '2');
MAP_NORM.put('¹', '1');
MAP_NORM.put('à', 'a');
MAP_NORM.put('á', 'a');
MAP_NORM.put('â', 'a');
MAP_NORM.put('ã', 'a');
MAP_NORM.put('ä', 'a');
MAP_NORM.put('è', 'e');
MAP_NORM.put('é', 'e');
MAP_NORM.put('ê', 'e');
MAP_NORM.put('ë', 'e');
MAP_NORM.put('í', 'i');
MAP_NORM.put('ì', 'i');
MAP_NORM.put('î', 'i');
MAP_NORM.put('ï', 'i');
MAP_NORM.put('ù', 'u');
MAP_NORM.put('ú', 'u');
MAP_NORM.put('û', 'u');
MAP_NORM.put('ü', 'u');
MAP_NORM.put('ò', 'o');
MAP_NORM.put('ó', 'o');
MAP_NORM.put('ô', 'o');
MAP_NORM.put('õ', 'o');
MAP_NORM.put('ö', 'o');
MAP_NORM.put('ñ', 'n');
MAP_NORM.put('ç', 'c');
}
if (value == null) {
return "";
}
StringBuilder sb = new StringBuilder(value);
for(int i = 0; i < value.length(); i++) {
Character c = MAP_NORM.get(sb.charAt(i));
if(c != null) {
sb.setCharAt(i, c.charValue());
}
}
return sb.toString();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32213 次 |
最近记录: |