How to add double quotes in a string in Java
Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such as
- Using an escape character
- Using the char keyword
- Using Unicode conversion characters
These ways can be implemented using source code
1. Escape character
We can add double quotes to the string using the escape sequence character.
The escape character used in this method is back slashed, i.e. ( \ ). It can be called an escape sequence character or escape character. The escape character '\' is used for adding double quotes. The main aim of this method is to insert double quotes at the initial and ending of the string.
Program to add double quotes using an escape character
// import io package
import java.io.*;
// Addquotes class
public class Addquotes{
// Main method
public static void main(String[] args)
{
// Static input string
String str = " \"Tutorialandexample is best platform to leran" ";
// Displaying the quoted string as output
System.out.println(str);
}
}
Output

2. Using char
can also use char for adding double quotes to string. This method must change
double quote(“) into char datatype. Consider the below example program single quote
indicates the character, and a double quote indicates a string. The double quote is converted into char; we need to add it with the given string to include double quotes.
Program to add double quotes using char
// Java program
// import io package for reading input and displaying output
import java.io.*;
// Quotes class
public class Quotes {
// Main method
public static void main(String[] args)
{
char value = '"';
String str
= value + "Students are intrested in Java Programming " + value;
System.out.println(str);
}
}
Output

3. Unicode Conversion Characters
Unicode characters are used to print characters or symbols. The encoded character \u0022 represents a double quote. To perform this operation, convert Unicode to char and add \u0022 to string. This Unicode contains 4 bytes. The standard encoding character UTF-8. There are different Unicode encodings like UTF-16 and UTF-8. Data types that can store characters are called char.
Program
public class Quotes {
public static void main(String[] args) {
String str = '\u0022' + "We love Javatpoint" + '\u0022';
System.out.println(str);
}
}
Output

These methods can be used for adding double quotes to the string.