Home » Java programming language

How to add double quotes to a string in Java?

Adding double quotes to a string: Here, we are going to learn how to add double quotes to a string in Java?
Submitted by Preeti Jain, on July 05, 2019

  • In Java, everything written in double-quotes is considered a string and the text written in double-quotes is display as it is.
  • Suppose, if we want to add double quotes to a string then we need [\"] escape sequence to escape quotes.
  • Let us suppose if we have a string "Preeti" and simply it will display like this preeti and if we want to add double quotes to string (i.e. display like "preeti" ) then we will write a statement like this "\"Preeti\"".

We will understand this thing with the help of an Example...

Example:

public class AddQuotesToString {
    public static void main(String[] args) {
        // Create a string named str1 with value Java 
        // and display like this Java.
        String str1 = "Java";
        
        // Create a string named str2 with value OOPS 
        // and display like this Java "OOPS".
        // For adding double quotes to OOPS  
        // then we need or add \" escape sequence to escape quotes 
        // around both side string.
        String str2 = " \" OOPS \" ";
        System.out.println("Display String without quotes " + " " + str1);
        System.out.println("Display String with quotes " + " " + str2);
    }
}

Output

D:\Programs>javac AddQuotesToString.java

D:\Programs>java AddQuotesToString
Display String without quotes  Java
Display String with quotes   " OOPS "





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.