×

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

  1. Using an escape character
  2. Using the char keyword
  3. 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

How to add double quotes in a string

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

How to add double quotes in a string

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

How to add double quotes in a string

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


Related Topics

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.