Say you have a String literal with a lot of quotation marks inside it. You could escape them all, but it's a pain, and difficult to read.
假设你有一个字符串文字,里面有很多引号。你可以逃脱它们,但这是一种痛苦,难以阅读。
In some languages, you can just do this:
在某些语言中,您可以这样做:
foo = '"Hello, World"';
In Java, however, ''
is used for char
s, so you can't use it for String
s this way. Some languages have syntax to work around this. For example, in python, you can do this:
但是,在Java中,''用于表示字符,所以你不能以这种方式将它用于字符串。有些语言有解决此问题的语法。例如,在python中,您可以这样做:
"""A pretty "convenient" string"""
Does Java have anything similar?
Java有类似的东西吗?
7 个解决方案
#1
64
The answer is no, and the proof resides in the Java Language Specification:
答案是否定的,证据在于Java语言规范:
StringLiteral:
"StringCharacters"
StringCharacters:
StringCharacter
| StringCharacters StringCharacter
StringCharacter:
InputCharacter but not " or \
| EscapeSequence
As you can see a StringLiteral
can just be bound by "
and cannot contain special character without escapes..
正如您所看到的,StringLiteral只能被“绑定”并且不能包含没有转义的特殊字符。
A side note: you can embed Groovy inside your project, this will extend the syntax of Java allowing you to use '''multi line string '''
, ' "string with single quotes" '
and also "string with ${variable}"
.
附注:您可以在项目中嵌入Groovy,这将扩展Java的语法,允许您使用'''多行字符串''','带单引号的字符串''以及带有$ {variable}的字符串”。
#2
116
No, and I've always been annoyed by the lack of different string-literal syntaxes in Java.
不,我一直对Java中缺少不同的字符串文字语法感到恼火。
Here's a trick I've used from time to time:
这是我不时使用的一个技巧:
String myString = "using `backticks` instead of quotes".replace('`', '"');
I mainly only do something like that for a static field. Since it's static the string-replace code gets called once, upon initialization of the class. So the runtime performance penalty is practically nonexistent, and it makes the code considerably more legible.
我主要只为静态字段做类似的事情。由于它是静态的,所以在初始化类时,字符串替换代码会被调用一次。因此,运行时性能损失实际上是不存在的,并且它使代码更加清晰。
#3
6
you can also use StringEscapeUtils from apache commons
你也可以使用apache commons中的StringEscapeUtils
UPDATE: If someone is interested in some examples here is a useful link : http://java.dzone.com/articles/commons-lang-3-improved-and-powerful-StringEscapeUtils
更新:如果某人对某些示例感兴趣,这里有一个有用的链接:http://java.dzone.com/articles/commons-lang-3-improved-and-powerful-StringEscapeUtils
#4
4
Simple answer: No.
简单回答:没有。
For longer strings that must be escaped, I usually read them from some external resource.
对于必须转义的较长字符串,我通常从一些外部资源中读取它们。
#5
3
There might be in a future version of Java (10 or more).
可能存在Java的未来版本(10或更多)。
See JEPS 8196004 from January 2018: ("JEP" is the "JDK Enhancement Program")
从2018年1月开始参见JEPS 8196004 :(“JEP”是“JDK增强计划”)
JEP draft: Raw String Literals
Add a new kind of literal, a raw string literal, to the Java programming language.
Like the traditional string literal, a raw string literal produces a String, but does not interpret string escapes and can span multiple lines of source code.将一种新的文字(原始字符串文字)添加到Java编程语言中。与传统的字符串文字一样,原始字符串文字生成一个String,但不解释字符串转义,并且可以跨越多行源代码。
So instead of:
所以代替:
Runtime.getRuntime().exec("\"C:\\Program Files\\foo\" bar");
String html = "<html>\n"
" <body>\n" +
" <p>Hello World.</p>\n" +
" </body>\n" +
"</html>\n";
System.out.println("this".matches("\\w\\w\\w\\w"));
You would be able to type:
你可以输入:
Runtime.getRuntime().exec(`"C:\Program Files\foo" bar"`);
String html = `<html>
<body>
<p>Hello World.</p>
</body>
</html>
`;
System.out.println("this".matches(`\w\w\w\w`));
Neat!
整齐!
But it is still just a draft: it will need to posted, submitted, be a candidate, and funded, before being completed and making it into the next JDK.
但它仍然只是一个草案:在完成并进入下一个JDK之前,它需要发布,提交,成为候选人并获得资助。
#6
-2
The following seems to work for me:
以下似乎对我有用:
String x = "Some Text" + '"' + "More Text" + '"' + "Even More Text";
I think because char is the primitive variable type for String, Strings and chars can be combined (at least the eclipse compiler doesn't seem to complain).
我认为因为char是String的原始变量类型,所以可以组合字符串和字符(至少eclipse编译器似乎没有抱怨)。
#7
-3
If you want to escape '
or "
in your string, you can use the following code:
如果要在字符串中转义“或”,可以使用以下代码:
String text = ...
text = text.replaceAll("('|\")", "\\\\$1");
#1
64
The answer is no, and the proof resides in the Java Language Specification:
答案是否定的,证据在于Java语言规范:
StringLiteral:
"StringCharacters"
StringCharacters:
StringCharacter
| StringCharacters StringCharacter
StringCharacter:
InputCharacter but not " or \
| EscapeSequence
As you can see a StringLiteral
can just be bound by "
and cannot contain special character without escapes..
正如您所看到的,StringLiteral只能被“绑定”并且不能包含没有转义的特殊字符。
A side note: you can embed Groovy inside your project, this will extend the syntax of Java allowing you to use '''multi line string '''
, ' "string with single quotes" '
and also "string with ${variable}"
.
附注:您可以在项目中嵌入Groovy,这将扩展Java的语法,允许您使用'''多行字符串''','带单引号的字符串''以及带有$ {variable}的字符串”。
#2
116
No, and I've always been annoyed by the lack of different string-literal syntaxes in Java.
不,我一直对Java中缺少不同的字符串文字语法感到恼火。
Here's a trick I've used from time to time:
这是我不时使用的一个技巧:
String myString = "using `backticks` instead of quotes".replace('`', '"');
I mainly only do something like that for a static field. Since it's static the string-replace code gets called once, upon initialization of the class. So the runtime performance penalty is practically nonexistent, and it makes the code considerably more legible.
我主要只为静态字段做类似的事情。由于它是静态的,所以在初始化类时,字符串替换代码会被调用一次。因此,运行时性能损失实际上是不存在的,并且它使代码更加清晰。
#3
6
you can also use StringEscapeUtils from apache commons
你也可以使用apache commons中的StringEscapeUtils
UPDATE: If someone is interested in some examples here is a useful link : http://java.dzone.com/articles/commons-lang-3-improved-and-powerful-StringEscapeUtils
更新:如果某人对某些示例感兴趣,这里有一个有用的链接:http://java.dzone.com/articles/commons-lang-3-improved-and-powerful-StringEscapeUtils
#4
4
Simple answer: No.
简单回答:没有。
For longer strings that must be escaped, I usually read them from some external resource.
对于必须转义的较长字符串,我通常从一些外部资源中读取它们。
#5
3
There might be in a future version of Java (10 or more).
可能存在Java的未来版本(10或更多)。
See JEPS 8196004 from January 2018: ("JEP" is the "JDK Enhancement Program")
从2018年1月开始参见JEPS 8196004 :(“JEP”是“JDK增强计划”)
JEP draft: Raw String Literals
Add a new kind of literal, a raw string literal, to the Java programming language.
Like the traditional string literal, a raw string literal produces a String, but does not interpret string escapes and can span multiple lines of source code.将一种新的文字(原始字符串文字)添加到Java编程语言中。与传统的字符串文字一样,原始字符串文字生成一个String,但不解释字符串转义,并且可以跨越多行源代码。
So instead of:
所以代替:
Runtime.getRuntime().exec("\"C:\\Program Files\\foo\" bar");
String html = "<html>\n"
" <body>\n" +
" <p>Hello World.</p>\n" +
" </body>\n" +
"</html>\n";
System.out.println("this".matches("\\w\\w\\w\\w"));
You would be able to type:
你可以输入:
Runtime.getRuntime().exec(`"C:\Program Files\foo" bar"`);
String html = `<html>
<body>
<p>Hello World.</p>
</body>
</html>
`;
System.out.println("this".matches(`\w\w\w\w`));
Neat!
整齐!
But it is still just a draft: it will need to posted, submitted, be a candidate, and funded, before being completed and making it into the next JDK.
但它仍然只是一个草案:在完成并进入下一个JDK之前,它需要发布,提交,成为候选人并获得资助。
#6
-2
The following seems to work for me:
以下似乎对我有用:
String x = "Some Text" + '"' + "More Text" + '"' + "Even More Text";
I think because char is the primitive variable type for String, Strings and chars can be combined (at least the eclipse compiler doesn't seem to complain).
我认为因为char是String的原始变量类型,所以可以组合字符串和字符(至少eclipse编译器似乎没有抱怨)。
#7
-3
If you want to escape '
or "
in your string, you can use the following code:
如果要在字符串中转义“或”,可以使用以下代码:
String text = ...
text = text.replaceAll("('|\")", "\\\\$1");