如何在 Java 中转义字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2406121/
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
How do I escape a string in Java?
提问by Marius
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?
我正在用 Java 读取源文件,但是当我打印它时(sysout),转义字符不再转义。如何在 Java 中转义字符串中的字符\n
和\t
字符?
采纳答案by schematic
You should use the StringEscapeUtils
class from Apache Commons Text(you can also find the class in Apache Commons Lang3 but that one is deprecated). 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.
您应该使用Apache Commons Text 中的StringEscapeUtils
类(您也可以在 Apache Commons Lang3 中找到该类,但该类已被弃用)。您会发现 Apache Commons 中有许多其他产品可能对您在 Java 开发中遇到的其他问题有用,因此您不必重新发明轮子。
The specific call you want has to do with "Java escaping"; the API call is StringEscapeUtils.escapeJava()
. For example:
您想要的特定调用与“Java 转义”有关;API 调用是StringEscapeUtils.escapeJava()
. 例如:
System.out.println(StringEscapeUtils.escapeJava("Hello\r\n\tW\"o\"rld\n")
would print out:
会打印出来:
Hello\r\n\tW\"o\"rld\n
There are plenty of other escaping utilities in that library as well. You can find Apache Commons Text in Maven Centraland you'd add it to your Maven project like this:
该库中还有许多其他转义实用程序。您可以在 Maven Central 中找到Apache Commons Text,然后将其添加到您的 Maven 项目中,如下所示:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.3</version>
</dependency>
and in case you are using Gradle:
如果您使用的是 Gradle:
compile "org.apache.commons:commons-text:1.3"
回答by codaddict
Using:
使用:
\\n
and \\t
\\n
和 \\t
Some characters preceded by a backslash (\
) form an escape sequenceand have special meaning to the compiler. So in your case \n
and \t
are treated as special (newline and tab respectively). So we need to escape the backslash to make n
and t
treated literally.
一些以反斜杠 ( \
)开头的字符形成转义序列,对编译器具有特殊意义。所以在你的情况下\n
,\t
被视为特殊的(分别是换行符和制表符)。所以我们需要转义反斜杠来制作n
和t
处理。
回答by polygenelubricants
Given String s
,
给定String s
,
s = s.replace("\", "\\");
Replaces all \
with \\
.
全部替换\
为\\
.
回答by Gopi
StringEscapeUtilsis good for string related operations
StringEscapeUtils适用于字符串相关操作
回答by Dan
Many of the solutions here suggest adding the Apache Commons Textand using StringEscapeUtils. Some of the other solutions here are simply wrong.
这里的许多解决方案都建议添加Apache Commons Text并使用StringEscapeUtils。这里的一些其他解决方案完全是错误的。
A potential solutions is as follows:
一个潜在的解决方案如下:
/**
* escape()
*
* Escape a give String to make it safe to be printed or stored.
*
* @param s The input String.
* @return The output String.
**/
public static String escape(String s){
return s.replace("\", "\\")
.replace("\t", "\t")
.replace("\b", "\b")
.replace("\n", "\n")
.replace("\r", "\r")
.replace("\f", "\f")
.replace("\'", "\'")
.replace("\"", "\\"");
}
The escape list is from Oracle's list. (Notethat \\
is escaped first as you don't want to re-escape it later.)
转义列表来自 Oracle 的list。(请注意,\\
它首先被转义,因为您不想稍后重新转义它。)
This solution isn't as fast as it could be, but it should work. Ideally you would only parse the String once and you wouldn't need to keep rebuilding your String array. For small Strings this should be fine.
这个解决方案没有想象的那么快,但它应该可以工作。理想情况下,您只需解析 String 一次,而无需继续重建 String 数组。对于小字符串,这应该没问题。
If you're thinking about this from the perspective of storing data, also consider something like converting it to Base64representation - it's fast, single parse and uses not too much extra space.
如果您是从存储数据的角度考虑这一点,还可以考虑将其转换为Base64表示之类的东西——它是快速的、单次解析的,并且不会使用太多的额外空间。