In Java, How to Write a String Literal Without Having to Escape Quotes

In Java, is there a way to write a string literal without having to escape quotes?

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

  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..

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}".

Java String use quotes without escape character

You can't. But if you are too lazy to escape each of the double quotes, there are some trick that can do that. For example:

String quotes = "....................".replace(".","\"");
System.out.println(quotes);

output: """"""""""""""""""""

Don't escape double quotes in a string

If you want to to test your script with Java, with parameters containing quotes, you don't have any choice, you'll have to escape it.

String[] command  = new String[] {
"path/executeScript",
"--path=" + p,
"--user=" + user,
"--html=\"anchor bold\"",
"--port=8081"
}
Runtime.getRuntime().exec(command);

Technical explanation : https://stackoverflow.com/a/3034195/2003986

Don't escape double quotes in a string

If you want to to test your script with Java, with parameters containing quotes, you don't have any choice, you'll have to escape it.

String[] command  = new String[] {
"path/executeScript",
"--path=" + p,
"--user=" + user,
"--html=\"anchor bold\"",
"--port=8081"
}
Runtime.getRuntime().exec(command);

Technical explanation : https://stackoverflow.com/a/3034195/2003986

Do You Need To Escape * Character in Java?

You also have to escape the backslash:

String delimiter = "\\*";

Because the string literal "\\" is just a single backslash, so the string literal "\\*" represents an escaped asterisk.

How to write string literals in python without having to escape them?

Raw string literals:

>>> r'abc\dev\t'
'abc\\dev\\t'

String literal without need to escape backslash

There is somewhat similar thing available in Ruby. E.g.

foo = %Q(The integer division operator in VisualBASIC is written "a \\ b" and #{'interpolation' + ' works'})

You can also interpolate strings in it. The only caveat is, you would still need to escape \ character.

HTH

How can I make Java print quotes, like Hello ?

System.out.print("\"Hello\"");

The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include:

  • Carriage return and newline: "\r" and "\n"
  • Backslash: "\\"
  • Single quote: "\'"
  • Horizontal tab and form feed: "\t" and "\f"

The complete list of Java string and character literal escapes may be found in the section 3.10.6 of the JLS.

It is also worth noting that you can include arbitrary Unicode characters in your source code using Unicode escape sequences of the form \uxxxx where the xs are hexadecimal digits. However, these are different from ordinary string and character escapes in that you can use them anywhere in a Java program ... not just in string and character literals; see JLS sections 3.1, 3.2 and 3.3 for a details on the use of Unicode in Java source code.

See also:

  • The Oracle Java Tutorial: Numbers and Strings - Characters

  • In Java, is there a way to write a string literal without having to escape quotes? (Answer: No)



Related Topics



Leave a reply



Submit