我在 Java 中使用以下代码来用波形符替换字符,例如:
á é í ó ú Á É Í Ó Ú à è ì ò ù À È Ì Ò Ù
text = text.replace( "á", "a" );
text = text.replace( "é", "e" );
text = text.replace( "Ã", "i" );
text = text.replace( "ó", "o" );
text = text.replace( "ú", "u" );
// caracteres raros: tildes mayusculas
text = text.replace( "Ã", "A" );
text = text.replace( "É", "E" );
text = text.replace( "Ã", "I" );
text = text.replace( "Ó", "O" );
text = text.replace( "Ú", "U" );
// caracteres raros: tildes inversas minusculas
text = text.replace( "Ã ", "a" );
text = text.replace( "è", "e" );
text = text.replace( "ì", "i" );
text = text.replace( "ò", "o" );
text = text.replace( "ù", "u" );
// caracteres raros: tildes inversas mayusculas
text = text.replace( "À", "A" );
text = text.replace( "È", "E" );
text = text.replace( "Ì", "I" );
text = text.replace( "Ã’", "O" );
text = text.replace( "Ù", "U" );
// caracteres raros: ñ minuscula y mayuscula
text = text.replace( "Ñ", "n" );
text = text.replace( "ñ", "N" );
我想使用如下符号:
text = text.replace( "\uD1232", "N" );
但我不知道在哪里可以找到包含这些字符的表格:... È, Ë, Ò ...
最佳答案
JDK 包含一个名为 native2ascii
的工具.
使用特殊字符以 UTF-8 编码创建文本文件。
例如文件in.txt
:
á é í ó ú Á É Í Ó Ú à è ì ò ù À È Ì Ò Ù
然后调用:
native2ascii -encoding UTF-8 in.txt out.txt
之后,您的文件 out.txt
包含如下转义序列:
\u00e1 \u00e9 \u00ed \u00f3 \u00fa \u00c1 \u00c9 \u00cd \u00d3 \u00da \u00e0 \u00e8 \u00ec \u00f2 \u00f9 \u00c0 \u00c8 \u00cc \u00d2 \u00d9
关于java - 如何将编码字符替换为字符串文字?像\uDXYZW 或者类似的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44011963/