如何将包含字符转义序列的字符串转换为字符? [英] How can I convert a string containing a character escape sequence into a char?
问题描述
我正在寻找一种将包含字符转义序列的字符串转换为表示字符的方法。
所以,例如,我想解析字符串 \
(其中有两个字符,一个反斜杠和一个双引号)放入char 中。所以,一个字符数组成一个字符。
所以可能会这样做的东西,反之亦然:
包测试;
public class Test {
private static char parseChar(String string){
char c = 0;
if(\\\\
.equals(string)){
c ='\\\
';
} else if(\\t.equals(string)){
c ='\t';
} else if(\\r.equals(string)){
c ='\r';
} else if(\\f.equals(string)){
c ='\f';
} else if(\\b.equals(string)){
c ='\b';
} else if(\\\\。equals(string)){
c ='\'';
} else if(\\\。equals(string)){
c ='\';
} else if(\\\\\.equals(string)){
c ='\\';
}
return c;
}
public static void main(String [] args){
for(String arg:args){
System.out.println(arg +:+ )parseChar(arg)+:+ parseChar(arg)+;);
}
}
}
我不敢相信有java.lang或其他可以为我提供好的(也许是本机的)代码,因为我觉得上面的代码可能是不完整的,而不是解析每个有问题的(escapable?)字符,因为我是一个noob。
我想要一个可以做与String构造函数相同的工具:
String st =\ ;
char ch = st.charAt(0);
ch输出:;
感谢您阅读本文,对不起,如果不清楚我会定期检查,如果问
PS :
当我运行上述代码:
java -classpath〜/ workspace / MacroRecorder / bin / test.Test \\\\
\\\t \\f \ \r \\b \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;; b...它输出
\\\
:10:
;
\\ t:9:;
\f:12:
;
; r:13:
\b:8:;
\':39: ';
\:34:;
\\:92:\;
但在Ecli pse,输出是完全不同的,具有相同的参数,特别是非常混乱。
解决方案Apache Commons使用StringEscapeUtils你想要unescapeJava方法我想: http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.html#unescapeJava(java.lang.String)
I'm looking for a way to convert a string that contains a character escape sequence into the represented character.
So, for instance, I want to parse the string
\"
(which has two characters, a backslash and a double-quote) into the char"
. So, an array of chars into one char.So something that might do something like this and vice versa:
package test; public class Test { private static char parseChar(String string) { char c = 0; if ("\\n".equals(string)) { c = '\n'; }else if ("\\t".equals(string)) { c = '\t'; }else if ("\\r".equals(string)) { c = '\r'; }else if ("\\f".equals(string)) { c = '\f'; }else if ("\\b".equals(string)) { c = '\b'; }else if ("\\\'".equals(string)) { c = '\''; }else if ("\\\"".equals(string)) { c = '\"'; }else if ("\\\\".equals(string)) { c = '\\'; } return c; } public static void main(String[] args) { for (String arg : args) { System.out.println(arg + " : " + (int)parseChar(arg) + " : " + parseChar(arg) + ";"); } } }
I could not believe there is nothing in java.lang or other that can provide me with good (maybe native) code for this because I feel the above code might be incomplete and not parse every problematic (escapable?) character, because well I'm a noob. I want a tool that can do the same thing as the String constructor :
String st = "\""; char ch = st.charAt(0);
ch output : ";
Thank you for reading this, I am sorry if not clear I will check regularly and correct if asked.
PS:
When I run the above code:
java -classpath ~/workspace/MacroRecorder/bin/ test.Test \\n \\t \\f \\r \\b \\\' \\\" \\\\;
...it outputs
\n : 10 : ; \t : 9 : ; \f : 12 : ; ;r : 13 : \b : 8 :; \' : 39 : '; \" : 34 : "; \\ : 92 : \;
But in Eclipse, the output is completely different with the same parameters especially the " is very messy.
解决方案Apache Commons to the rescue with StringEscapeUtils, you want the unescapeJava method I think: http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.html#unescapeJava(java.lang.String)
这篇关于如何将包含字符转义序列的字符串转换为字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!