如何在Java中转义字符串? [英] How do I escape a string in Java?

查看:1957
本文介绍了如何在Java中转义字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Java中读取源文件,但是当我打印它(sysout)时,转义的字符不再被转义。如何在Java中的字符串中转义字符,如 \\\
\t

解决方案

您应该使用着名的Apache Commons中的StringEscapeUtils库。您会发现Apache Commons中有很多其他产品可能对您在Java开发中的其他问题有用,因此您不要重新发明。



您想要的具体调用与Java转义有关; API调用是StringEscapeUtils.escapeJava()。在该图书馆里还有很多其他的转义工具。



http://commons.apache.org/lang/api-release/org/apache/commons/lang3/ StringEscapeUtils.html#escapeJava(java.lang.String)


I'm reading a sourcefile in Java, but when I print it (sysout), the escaped characters are no longer escaped. How can I escape characters like \n and \t in a string in Java?

解决方案

You should use the StringEscapeUtils library from the well-known Apache Commons. You'll find that there are plenty of other offerings in Apache Commons that might serve useful for other problems you have in Java development, so that you don't reinvent the wheel.

The specific call you want has to do with "Java escaping"; the API call is StringEscapeUtils.escapeJava(). There are plenty of other escaping utilities in that library as well.

http://commons.apache.org/lang/api-release/org/apache/commons/lang3/StringEscapeUtils.html#escapeJava(java.lang.String)

这篇关于如何在Java中转义字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆