In Java, is there a way to write a string literal without having to hide quotation marks?

Let's say you have a string literal with lots of quotes inside it. You could have avoided them all, but it is pain and hard to read.

In some languages, you can simply do this:

foo = '"Hello, World"'; 

In Java, however, '' used for char s, so you cannot use it for String in this way. Some languages ​​have syntax to get around this. For example, in python you can do this:

 """A pretty "convenient" string""" 

Does Java have something similar?

+85
java string escaping
Jun 13 '10 at 22:45
source share
7 answers

The answer is no, and the proof is in the Java Language Specification :

  StringLiteral: "StringCharacters" StringCharacters: StringCharacter | StringCharacters StringCharacter StringCharacter: InputCharacter but not " or \ | EscapeSequence 

As you can see, StringLiteral can simply be anchored " and cannot contain a special character without screens.

Note: you can embed Groovy in your project, this will expand the Java syntax, allowing the use of '''multi line string ''' , ' "string with single quotes" ' , as well as "string with ${variable}" .

+59
Jun 13 '10 at 22:48
source share

No, and I was always annoyed by the lack of different string literal syntaxes in Java.

Here is a trick I used from time to time:

 String myString = "using `backticks` instead of quotes".replace('`', '"'); 

Basically I am doing something similar for a static field. Since it is static, the string replacement code is called once, after the class is initialized. Thus, there is practically no performance limitation at runtime, and this makes the code much more legible.

+108
Jun 13 '10 at 22:53
source share

you can also use StringEscapeUtils from apache commons

UPDATE If anyone is interested in some examples, there is a useful link here: http://java.dzone.com/articles/commons-lang-3-improved-and-powerful-StringEscapeUtils

+7
03 Sep
source share

The simple answer is no.

For longer lines that need to be escaped, I usually read them from some external resource.

+4
Jun 13 '10 at 22:49
source share

Perhaps in a future version of Java (10 or more).

See JEPS 8196004 from January 2018: ( “JEP” is “JDK Enhancement Program” )

JEP draft: Raw String Literals

Add a new literal type, a string literal, to the Java programming language.
As in a traditional string literal, a string literal builds a string, but does not interpret the string escape lines and can span multiple lines of source code .

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 could enter:

 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`)); 

Well maintained!

But it’s still just a project : he will need to publish, submit, be a candidate and be funded until its completion and turning it into the next JDK.

+2
Jan 27 '18 at 23:23
source share

If you want to avoid the ' or " in your line, you can use the following code:

 String text = ... text = text.replaceAll("('|\")", "\\\\$1"); 
-2
Jul 16 '13 at 11:22
source share

It seems to me that the following works:

 String x = "Some Text" + '"' + "More Text" + '"' + "Even More Text"; 

I think because char is a primitive variable type for String, strings and characters can be concatenated (at least the eclipse compiler does not seem to complain).

-2
Aug 23 '13 at 12:12
source share



All Articles