3
4

3 回答 3

4

There is the Apache Commons StringEscapeUtils which has HTML encoding. This encoding is pretty close to what you may need

String escaped code = StringEscapeUtils.escapeHtml(rowId)

See doc

于 2013-09-12T20:25:05.500 回答
3

Use Integer.toHexString((int)x.charAt(34));, you can get the string of the unicode char, and add \\u before it, you will get the String.

public static String removeUnicode(String input){
    StringBuffer buffer = new StringBuffer(input.length());
    for (int i =0; i < input.length(); i++){
        if ((int)input.charAt(i) > 256){
        buffer.append("\\u").append(Integer.toHexString((int)input.charAt(i)));
        } else {
            if ( input.charAt(i) == '\n'){
                buffer.append("\\n");
            } else {
                buffer.append(input.charAt(i));
            }
        }
    }
    return buffer.toString();
}
于 2013-06-14T08:20:33.227 回答
-1
String original = "String containning special chars  \u202C \n  \u202C  \u202C  \u202C";
String escaped = original.replace("\u202C", "\\u202C");
System.out.println(escaped);
于 2013-06-14T08:00:00.390 回答