×

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

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

Union in Java

Sets.union() method in Java returns an immutable representation of both the union of two sets. Every element that is present in either backup set is included in the set that...

2 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Big Decimal class in Java

The fairly Big pretty Decimal class provides operations for arithmetic, rounding, comparison and format conversion in a sort of big way. It can handle generally large and very small floating-point...

6 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Tilt operator in Java | Tilde operator in Java Example

The symbol designates it as a unary operator (pronounced as the tilde). It gives back the bit's complement or inverse. Every 0 turns into a 1, and every 1 back...

3 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

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...

2 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Java FileOutputStream

What is FileOutputStream?When we need raw stream data written into a file, we need to look for another option: FileOutputStream. It is used when the file's data is byte-oriented. It comes under...

4 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.