java - 如何将编码字符替换为字符串文字?像\uDXYZW 或者类似的东西?

标签 java string text unicode utf-8

我在 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/

相关文章:

java - 指示 wsimport/jaxb 使用现有的模型/域类

java - 无效赋值运算符错误 >= Java

java - OpenCSV:将嵌套 Bean 映射到 CSV 文件

javascript - 如何从 javascript 字符串末尾删除可变数量的字符

python - 在 Python 中比较字符串和数字

file - Freebase 中所有标题/主题标题的文本文件

java - 在 Eclipse 中更改文本颜色

arrays - Swift 字符串数组的索引号

javascript - 在两个文本字段中显示完全相同的选择范围

javascript - PHP/Javascript 中的文本修改