Within Python I can use:
test = """My "cute" string"""
Where the double quotes get escaped. Is there a way that I can emulate this within Java without using escape slashes?
Within Python I can use:
test = """My "cute" string"""
Where the double quotes get escaped. Is there a way that I can emulate this within Java without using escape slashes?
I don't see a reason not to escape "
, but.. you can do:
System.out.println((char)34+"Some Text"+(char)34); //See ASCII table for more information
Output >> "Some Text"
If you don't need an escape, you should use string concatenation such as:
test = '"' + "My " + '"' + "cute" + '"' + " string" + '"';
Or using StringBuffer
to append "
character if you want.
Another way to do that is using String.format
:
test = String.format("%1$sMy %1$scute%1$s string%1$s", '"');
Which replaces %1$s
by your give "quote" later.
Beginning with Java 13, Java now supports triple-quotes! See JEP 355 for details.
You have to use \u0022
.
String str= "\u0022 MyString \u0022";
But this is quite similar to escaping "
by \
No, there is no sensible way of doing that in Java. Judging from the other answers, I'd say that escaping the quotes is the path of least pain.
It is possible in Groovy (which also runs on the Java VM), though, using either single quotes:
`'some "String"'`
or the so-called here documents:
""" Everything here can be "quoted"
/"""
But Java has no such mechanism.