需要使用è转换Java字符串;使用Java访问\u00E8

需要使用è转换Java字符串;使用Java访问\u00E8,java,unicode,Java,Unicode,我有一个Java字符串对象,它包含一个类似于“resumè”的单词,或者任何包含国际字符的单词。我要做的是将其转换为ASCII字符串中的非ASCII字符编码,如“resum\u00E8”。如何使用Java实现这一点?尝试使用String.replace()将一个字符转换为其他字符。也可以查看replaceAll()方法。下面是一个简单的实现(基于java.util.Properties.saveConvertprivate方法): private static final char[]hexDi

我有一个Java字符串对象,它包含一个类似于“resumè”的单词,或者任何包含国际字符的单词。我要做的是将其转换为ASCII字符串中的非ASCII字符编码,如“resum\u00E8”。如何使用Java实现这一点?

尝试使用String.replace()将一个字符转换为其他字符。也可以查看replaceAll()方法。

下面是一个简单的实现(基于
java.util.Properties.saveConvert
private方法):

private static final char[]hexDigit={'0','1','2','3','4','5','6',
‘7’、‘8’、‘9’、‘A’、‘B’、‘C’、‘D’、‘E’、‘F’};
公共静态字符串escapeUnicode(字符串str){
StringBuilder sb=新的StringBuilder();
对于(int i=0;i0x007e)){
某人附加(“\\”);
某人加上('u');
sb.附加(六位数字[(aChar>>12)&0xF)];
sb.附加(六位数字[(aChar>>8)和0xF)];
sb.附加(六位数字[(aChar>>4)和0xF)];
sb.附加(六位数字[(aChar&0xF)]);
}否则{
某人(阿喀尔);
}
}
使某人返回字符串();
}

您可以使用以下代码将è转换为\u00E8:

 class A {

public static void main(String[] args) {
     String data="è";
     String a = Converter.Uni2JavaLiteral(data);
     System.out.println("a=" + a);
    }

  }

 class Converter {
 private static char hexdigit(int c) {
    String charset = "0123456789ABCDEF";
    return charset.charAt(c & 0x0F);
}

private static String buildLiteral(char c) {
    String literal = hexdigit(c >>> 12) + "" + hexdigit(c >>> 8) + "" + hexdigit(c >>> 4) + "" + hexdigit(c);
    return literal;
}

public static String Uni2JavaLiteral(String input) {

    String literals = "";

    for (int i = 0; i < input.length(); i++) {
        if (input.charAt(i) == 10) {
            literals += "\\n";
        } else if (input.charAt(i) == 13) {
            literals += "\\r";
        } else if (input.charAt(i) == 92) {
            literals += "\\\\";
        } else if (input.charAt(i) == ' ') {
            literals += " ";
        } else if (input.charAt(i) < 32 || input.charAt(i) > 126) {
            literals += "\\u" + buildLiteral(input.charAt(i));
        } else {
            literals += input.charAt(i);
        }
    }
    return literals;
  }
A类{
公共静态void main(字符串[]args){
字符串数据=“è”;
字符串a=Converter.Uni2JavaLiteral(数据);
System.out.println(“a=“+a”);
}
}
类转换器{
专用静态字符十六进制数字(int c){
字符串charset=“0123456789ABCDEF”;
返回charset.charAt(c&0x0F);
}
私有静态字符串buildLiteral(字符c){
字符串文字=六位数字(c>>>12)+''+六位数字(c>>>8)+''+六位数字(c>>>4)+''+六位数字(c);
返回文本;
}
公共静态字符串Uni2JavaLiteral(字符串输入){
字符串文字=”;
对于(int i=0;i126){
文字+=“\\u”+buildLiteral(input.charAt(i));
}否则{
文字+=输入字符(i);
}
}
返回文本;
}

}

您可以使用下面的实用工具方法找到字符的unicode值

private static String findUnicodeValue(char ch) {
    return "\\u" + Integer.toHexString(ch | 0x10000).substring(1);
}

然后可以用unicode值替换字符。

这里有一个Java 8的替代方案:

  public static String encode(int ch)
  {
    return (ch >= 32 && ch < 127)
        ? Character.toString((char)ch)
            : String.format("\\u%04X", ch);
  }

  public static String encode(String s)
  {
    return s.chars().mapToObj(ch -> encode(ch)).collect(Collectors.joining());
  }
公共静态字符串编码(int-ch)
{
返回(ch>=32&&ch<127)
?Character.toString((char)ch)
:String.format(“\\u%04X”,ch);
}
公共静态字符串编码(字符串s)
{
返回s.chars().mapToObj(ch->encode(ch)).collect(Collectors.joining());
}

继承Tagir Valeev从java.util.Properties中学习的想法:

    package empty;

    public class CharsetEncode {

        public static void main(String[] args) {
            String s = "resumè";
            System.out.println(decompose(s));
        }

        public static String decompose(String s) {
            return saveConvert(s, true, true);
        }

        private static String saveConvert(String theString, boolean escapeSpace, boolean escapeUnicode) {
            int len = theString.length();
            int bufLen = len * 2;
            if (bufLen < 0) {
                bufLen = Integer.MAX_VALUE;
            }
            StringBuffer outBuffer = new StringBuffer(bufLen);

            for (int x = 0; x < len; x++) {
                char aChar = theString.charAt(x);
                // Handle common case first, selecting largest block that
                // avoids the specials below
                if ((aChar > 61) && (aChar < 127)) {
                    if (aChar == '\\') {
                        outBuffer.append('\\');
                        outBuffer.append('\\');
                        continue;
                    }
                    outBuffer.append(aChar);
                    continue;
                }
                switch (aChar) {
                case ' ':
                    if (x == 0 || escapeSpace)
                        outBuffer.append('\\');
                    outBuffer.append(' ');
                    break;
                case '\t':
                    outBuffer.append('\\');
                    outBuffer.append('t');
                    break;
                case '\n':
                    outBuffer.append('\\');
                    outBuffer.append('n');
                    break;
                case '\r':
                    outBuffer.append('\\');
                    outBuffer.append('r');
                    break;
                case '\f':
                    outBuffer.append('\\');
                    outBuffer.append('f');
                    break;
                case '=': // Fall through
                case ':': // Fall through
                case '#': // Fall through
                case '!':
                    outBuffer.append('\\');
                    outBuffer.append(aChar);
                    break;
                default:
                    if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode) {
                        outBuffer.append('\\');
                        outBuffer.append('u');
                        outBuffer.append(toHex((aChar >> 12) & 0xF));
                        outBuffer.append(toHex((aChar >> 8) & 0xF));
                        outBuffer.append(toHex((aChar >> 4) & 0xF));
                        outBuffer.append(toHex(aChar & 0xF));
                    } else {
                        outBuffer.append(aChar);
                    }
                }
            }
            return outBuffer.toString();
        }

        private static char toHex(int nibble) {
            return hexDigit[(nibble & 0xF)];
        }

        /** A table of hex digits */
        private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    }
包为空;
公共类字符集编码{
公共静态void main(字符串[]args){
字符串s=“resumè”;
System.out.println(分解);
}
公共静态字符串分解(字符串s){
返回saveConvert(s,true,true);
}
私有静态字符串saveConvert(字符串、布尔转义空间、布尔转义代码){
int len=字符串长度();
int bufLen=len*2;
如果(bufLen<0){
bufLen=整数的最大值;
}
StringBuffer Exputffer=新的StringBuffer(bufLen);
对于(int x=0;x61)和(&(亚喀尔<127)){
如果(aChar=='\\'){
exputffer.append('\\');
exputffer.append('\\');
继续;
}
外加剂(aChar);
继续;
}
开关(aChar){
案例“”:
如果(x==0 | | escapeSpace)
exputffer.append('\\');
附加(“”);
打破
案例'\t':
exputffer.append('\\');
exputffer.append('t');
打破
案例“\n”:
exputffer.append('\\');
exputffer.append('n');
打破
案例'\r':
exputffer.append('\\');
exputffer.append('r');
打破
案例'\f':
exputffer.append('\\');
附加('f');
打破
案例“=”://失败
案例“:”://失败
案例“#”://失败
案例“!”:
exputffer.append('\\');
外加剂(aChar);
打破
违约:
if((亚喀尔<0x0020)| |(亚喀尔>0x007e))&escapeUnicode){
exputffer.append('\\');
exputffer.append('u');
exputffer.append(toHex((aChar>>12)和0xF));
exputffer.append(toHex((aChar>>8)和0xF));
exputffer.append(toHex((aChar>>4)和0xF));
exputffer.append(toHex(aChar&0xF));
}否则{
外加剂(aChar);
}
}
}
返回exputfer.toString();
}
私有的
    package empty;

    public class CharsetEncode {

        public static void main(String[] args) {
            String s = "resumè";
            System.out.println(decompose(s));
        }

        public static String decompose(String s) {
            return saveConvert(s, true, true);
        }

        private static String saveConvert(String theString, boolean escapeSpace, boolean escapeUnicode) {
            int len = theString.length();
            int bufLen = len * 2;
            if (bufLen < 0) {
                bufLen = Integer.MAX_VALUE;
            }
            StringBuffer outBuffer = new StringBuffer(bufLen);

            for (int x = 0; x < len; x++) {
                char aChar = theString.charAt(x);
                // Handle common case first, selecting largest block that
                // avoids the specials below
                if ((aChar > 61) && (aChar < 127)) {
                    if (aChar == '\\') {
                        outBuffer.append('\\');
                        outBuffer.append('\\');
                        continue;
                    }
                    outBuffer.append(aChar);
                    continue;
                }
                switch (aChar) {
                case ' ':
                    if (x == 0 || escapeSpace)
                        outBuffer.append('\\');
                    outBuffer.append(' ');
                    break;
                case '\t':
                    outBuffer.append('\\');
                    outBuffer.append('t');
                    break;
                case '\n':
                    outBuffer.append('\\');
                    outBuffer.append('n');
                    break;
                case '\r':
                    outBuffer.append('\\');
                    outBuffer.append('r');
                    break;
                case '\f':
                    outBuffer.append('\\');
                    outBuffer.append('f');
                    break;
                case '=': // Fall through
                case ':': // Fall through
                case '#': // Fall through
                case '!':
                    outBuffer.append('\\');
                    outBuffer.append(aChar);
                    break;
                default:
                    if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode) {
                        outBuffer.append('\\');
                        outBuffer.append('u');
                        outBuffer.append(toHex((aChar >> 12) & 0xF));
                        outBuffer.append(toHex((aChar >> 8) & 0xF));
                        outBuffer.append(toHex((aChar >> 4) & 0xF));
                        outBuffer.append(toHex(aChar & 0xF));
                    } else {
                        outBuffer.append(aChar);
                    }
                }
            }
            return outBuffer.toString();
        }

        private static char toHex(int nibble) {
            return hexDigit[(nibble & 0xF)];
        }

        /** A table of hex digits */
        private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    }