Geo*_*Geo 21 java string unicode
假设我有一个包含Ü的字符串.我怎么能找到所有那些unicode字符?我应该测试他们的代码吗?我该怎么办?
例如,给定字符串"AÜXÜ",我想将其转换为"AYXY".我想对其他unicode角色做同样的事情,我不想将它们存储在某种翻译地图中.
Bal*_*usC 15
"unicode characters"的定义含糊不清,但将被视为标准ISO 8859字符集未涵盖的UTF-8 字符.如果在您的情况下这是真的,则循环遍历String中的所有字符并测试其代码点以确定它是否在给定的字符集内.
或者,Map<Character, Character>在地图中使用包含匹配键的字符和字符.例如:
Map<Character, Character> charReplacementMap = new HashMap<Character, Character>() {{
put('Ü', 'Y');
// Put more here.
}};
String originalString = "AÜAÜ";
StringBuilder builder = new StringBuilder();
for (char currentChar : originalString.toCharArray()) {
Character replacementChar = charReplacementMap.get(currentChar);
builder.append(replacementChar != null ? replacementChar : currentChar);
}
String newString = builder.toString();
Run Code Online (Sandbox Code Playgroud)
或者,你的意思是"所有带变音符号的人物"?如果是,则使用java.text.Normalizer删除变音标记:
/**
* Remove any diacritical marks (accents like ç, ñ, é, etc) from
* the given string (so that it returns plain c, n, e, etc).
* @param string The string to remove diacritical marks from.
* @return The string with removed diacritical marks, if any.
*/
public static String removeDiacriticalMarks(String string) {
return Normalizer.normalize(string, Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
Run Code Online (Sandbox Code Playgroud)
一个陷阱,Ü将成为U,而不是Y.不确定这是否是你所追求的.如果你想用发音字符替换,你真的需要创建一个映射.当然,这是一项繁琐的工作,但它的完成时间比你需要的时间少.
jit*_*ter 14
你可以遍历你的字符串和每个字符调用
If (Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN) {
// replace with Y
}
Run Code Online (Sandbox Code Playgroud)
msp*_*msp 12
你可以反过来问问这个角色是不是ascii角色.
public static boolean isAscii(char ch) {
return ch < 128;
}
Run Code Online (Sandbox Code Playgroud)
当然,您必须通过char分析字符串char.
(该方法来自commons-lang Charutils,其中包含大量有用的Character方法)
| 归档时间: |
|
| 查看次数: |
35560 次 |
| 最近记录: |