将Java字符串转换为ascii [英] Converting Java String to ascii

查看:392
本文介绍了将Java字符串转换为ascii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 String s转换为 String 没有那些特殊字母(在这种情况下是HASTDJUR)。我怎么能用Java做呢?感谢您的帮助!

I need to convert Strings 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:

Java - 摆脱重音并将它们转换成普通字母

因此答案也是一样的:

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'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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆