将Java字符串转换为ascii [英] Converting Java String to ascii
问题描述
我需要将 String
s转换为 String $,其中包含特定于某些语言的某些字母(如HÄSTDJUR - noteÄ) c $ c>没有那些特殊字母(在这种情况下是HASTDJUR)。我怎么能用Java做呢?感谢您的帮助!
I need to convert String
s that consists of some letters specific to certain languages (like HÄSTDJUR - note Ä) to a String
without those special letters (in this case HASTDJUR). How can I do it in Java? Thanks for help!
这听起来并不是真的。方案如下 - 您想要使用该应用程序,但没有瑞典语键盘。因此,您不必查看字符映射,而是使用拉丁字母表中的典型字母替换特殊字母。
It is not really about how it sounds. The scenario is following - you want to use the application, but don't have the Swedish keyboard. So instead of looking at the character map, you type it by replacing special letters with the typical letters from the latin alphabet.
推荐答案
我认为您的问题与此问题相同:
I think your question is the same as this one:
因此答案也是一样的:
String convertedString =
Normalizer
.normalize(input, Normalizer.Form.NFD)
.replaceAll("[^\\p{ASCII}]", "");
参考文献
参见
References
See
- JavaDoc:Normalizer.normalize(String,Normalizer.Form)
- JavaDoc:Normalizer.Form.NFD
- Sun Java Tutorial :Normalizer的API
- JavaDoc: Normalizer.normalize(String, Normalizer.Form)
- JavaDoc: Normalizer.Form.NFD
- Sun Java Tutorial: Normalizer's API
final String input = "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ";
System.out.println(
Normalizer
.normalize(input, Normalizer.Form.NFD)
.replaceAll("[^\\p{ASCII}]", "")
);
输出:
这是一个时髦的字符串
This is a funky String
这篇关于将Java字符串转换为ascii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!