Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double quotes within string like triple double quote in Python

Tags:

java

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?

like image 712
Lucas Kauffman Avatar asked Oct 29 '25 14:10

Lucas Kauffman


2 Answers

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"

like image 53
Maroun Avatar answered Nov 01 '25 05:11

Maroun


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.

like image 45
Tu Tran Avatar answered Nov 01 '25 03:11

Tu Tran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!