将 Java 字符串转换为 ascii
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3707977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Java String to ascii
提问by grem
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!
我需要将String
包含某些特定于某些语言的字母(如 H?STDJUR - 注意?)的 s转换为String
没有这些特殊字母的 s(在本例中为 HASTDJUR)。我怎样才能在 Java 中做到这一点?感谢帮助!
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.
这与听起来如何无关。场景如下 - 您想使用该应用程序,但没有瑞典语键盘。因此,不是查看字符映射表,而是通过用拉丁字母表中的典型字母替换特殊字母来键入它。
采纳答案by Sean Patrick Floyd
I think your question is the same as this one:
我想你的问题和这个一样:
Java - getting rid of accents and converting them to regular letters
and hence the answer is also the same:
因此答案也是一样的:
Solution
解决方案
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
- JavaDoc: Normalizer.normalize(String, Normalizer.Form)
- JavaDoc:Normalizer.Form.NFD
- Sun Java 教程:Normalizer 的 API
Example Code:
示例代码:
final String input = "T??? ?? a f?ň?? ????ń?";
System.out.println(
Normalizer
.normalize(input, Normalizer.Form.NFD)
.replaceAll("[^\p{ASCII}]", "")
);
Output:
输出:
This is a funky String
这是一个时髦的字符串
回答by Noel M
I'd suggest a mapping, of special characters, to the ones you want.
我建议将特殊字符映射到您想要的字符。
? --> A
é --> e
A --> A (exactly the same)
etc...
And then you can just call your mapping over your text (in pseudocode):
然后你可以在你的文本上调用你的映射(用伪代码):
for letter in string:
newString += map(letter)
Effectively, you need to create a set of rules for what character maps to the ASCII equivalent.
实际上,您需要为哪些字符映射到 ASCII 等效项创建一组规则。