Home java How do I insert a double quote character in a string?

How do I insert a double quote character in a string?

Author

Date

Category

Example:

String str1 = "String with" word "in quotes";

How do I correctly insert the quotation marks (“)?


Answer 1

final char dm = (char) 34;
final String string = "STRING:" + dm + "string" + dm;
System.out.println (string);

Output:

STRING: “string”


Answer 2, authority 90%

The most standard way used in many languages, including Java , is escaping using the \ character:

String myString = "String with the \" word \ "in quotes";

Answer 3, authority 98%

String str1 = "A string with the \" word \ "in quotes";

Answer 4, authority 98%

Yes, there is, you add \ before each character to be escaped.
For example, to display "C: \\\ folder \\\ folder1 \" , you need to add on the “screen” before EACH character like this:

System.out.println ("\" C: \\\\\\\ folder \\\\\ \\ folder1 \\\\\ "");

Answer 5, authority 100%

String x = "\\";
  String y = "\" ";
  System.out.println ("It's Windows path:" + y + "C:" + x + "Program Files" + x + "Java" + x + "jdk1.7.0" + x + "bin" + y);
  System.out.println ("It's Java string:" + x + y + "C:" + x + x + "Program Files" + x + x + "Java" + x + x + "jdk1.7.0" + x + x + "bin" + x + y);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions