java 打印带有转义不可打印字符的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17103660/
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
Print String with escape non printable characters
提问by MaVRoSCy
I have this String
我有这个字符串
String x="String containning special chars \u202C \n \u202C \u202C \u202C";
How can I print out this: String containning special chars \u202C \n \u202C \u202C \u202C
?
我怎样才能打印出这个:String containning special chars \u202C \n \u202C \u202C \u202C
?
Tried
试过
System.out.println(x.replace("\","\\"));
But that only prints String containning special chars ? \n ? ? ?
但这只能打印 String containning special chars ? \n ? ? ?
Also tried
也试过
String out = org.apache.commons.lang3.StringEscapeUtils.unescapeJava(x);
System.out.println(out);
But that also doesn't help.
但这也无济于事。
Any one with a suggestion or an API that I am not aware of?
有人提出我不知道的建议或 API 吗?
UPDATE - SOLUTION
更新 - 解决方案
Following @lbear aproach I came up with this functions that deals most cases of escaped Strings
遵循@lbear aproach,我想出了这个处理大多数转义字符串情况的函数
public static String removeUnicodeAndEscapeChars(String input) {
StringBuilder buffer = new StringBuilder(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 if(input.charAt(i) == '\t'){
buffer.append("\t");
}else if(input.charAt(i) == '\r'){
buffer.append("\r");
}else if(input.charAt(i) == '\b'){
buffer.append("\b");
}else if(input.charAt(i) == '\f'){
buffer.append("\f");
}else if(input.charAt(i) == '\''){
buffer.append("\'");
}else if(input.charAt(i) == '\"'){
buffer.append("\");
}else if(input.charAt(i) == '\'){
buffer.append("\\");
}else {
buffer.append(input.charAt(i));
}
}
}
return buffer.toString();
}
回答by Brig
There is the Apache CommonsStringEscapeUtilswhich has HTML encoding. This encoding is pretty close to what you may need
有具有 HTML 编码的Apache Commons StringEscapeUtils。这种编码非常接近您可能需要的
String escaped code = StringEscapeUtils.escapeHtml(rowId)
回答by lbear
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
.
使用Integer.toHexString((int)x.charAt(34));
,可以得到unicode字符的字符串,在\\u
前面加上,就得到了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();
}
回答by JB Nizet
String original = "String containning special chars \u202C \n \u202C \u202C \u202C";
String escaped = original.replace("\u202C", "\u202C");
System.out.println(escaped);