Quotes in a String Java

Quotes in a String Java | The Quotes are usually used to represent a string, but when we print the string then only the value will be printed without any quotes. In this post, our aim is to print quotes with the string. This blog mainly concentrates on how to include double quotes in a string in Java, and how to remove double quotes from JSON string in Java.

We can print quotes with strings by using the below-listed methods:-
1. By using Unicode characters
2. By using an escape sequence
3. By using char

Program to Put Quotes in a String Java

Here, we are using escape characters to print the quotes. The escape character \” is used to represent the quote and \’ is used to represent the single quote. Learn more about Escape Sequence in Java.

Java Escape Double Quotes In String Variable

public class Main {
   public static void main(String[] args) {
      String str = "\"Java Programming Language\"";
      System.out.println(str);

      String str1 = "\'Java Programming Language\'";
      System.out.println(str1);
   }
}

Output:-

“Java Programming Language”
‘Java Programming Language’

Java Double Quotes In String using char

We can take a char variable and assign a double quote to it. In this way, we can print string with double quotes in Java without using an escape sequence. Let us see how to print string with double quotes in Java using char without escape sequence.

public class Main{
   public static void main(String[] args) {
      char doubleQuote = '"';
      String string = doubleQuote + 
          "Java Programming Language" + doubleQuote;
      System.out.println(string);
   }
}

Output:-

“Java Programming Language”

But the same thing is not possible with single quotes. The character is assigned using a single quote, therefore, ''' will give an error. To solve this problem we can use String. Below program to include single quotes in a string Java.

public class Main{
   public static void main(String[] args) {
      String doubleQuote = "'";
      String string = doubleQuote + 
           "Java Programming Language" + doubleQuote;
      System.out.println(string);
   }
}

Output:-

‘Java Programming Language’

How to Include Double Quotes in a String in Java using Unicode Character

This code explains how to print string with double quotes in Java by using Unicode characters. The Unicode stands for universal characters code, which contains all countries speaking languages’ character codes. Unicode character set is used for developing internationalization (I18N) applications. See more:- Unicode character set in Java

public class Main {
   public static void main(String[] args) {
      String string = '\u0022' + 
          "Java Programming Language" + '\u0022';
      System.out.println(string);
   }
}

Output:

“Java Programming Language”

In a very similar way, we can put single quotes in a string Java. The 0022 Unicode value represents the double quote (“) ASCII character, whereas 0027 represents the single quote character.

UnicodeASCII Character
'\u0022'"
'\u0027''
public class Main {
   public static void main(String[] args) {
      String string = "\u0027" + 
          "Java Programming Language" + "\u0027";
      System.out.println(string);
   }
}

Output:-

‘Java Programming Language’

How to Remove Double Quotes From String in Java

In this section let us see how to remove double quotes from string in Java. String JSON contains double quotes, and we may need to remove them from the string. For this purpose, we can take help from the replace() method of the String class.

public class Main {
   public static void main(String args[]) {
      String str = "\"Java Programming\"";
      System.out.println("The entered String is: " + str);
      String str1 = str.replace("\"", "");
      System.out.println("String after removing" + 
             " double quotes is: " + str1);
   }
}

Output:

The entered String is: “Java Programming”
String after removing double quotes is: Java Programming

In a very similar way, we can remove the single quote from the given string. Here also we will use replace() method to remove the single quote from the string. When we pass an empty string as the second parameter in the replace() method then it removes the first parameter from the given string.

public class Main {
   public static void main(String args[]) {
      String str = "\'Java Programming\'";
      System.out.println("The entered String is: " + str);
      String str1 = str.replace("\'", "");
      System.out.println("String after removing "+
                   "double quotes is: " + str1);
   }
}

The entered String is: ‘Java Programming’
String after removing double quotes is: Java Programming

Java Quotes Inside Quotes

Sometimes we need to put quotes inside quotes. In that case, we can use the above methods, or an escape sequence could be the easiest way to solve the problem. Example: John told me, “Amelia said, ‘It won’t work anyway.’”

public class Main{
  public static void main(String args[]) {
    String str = "\"Java 'Programming'\"";
    System.out.println("String1: " + str);
    
    String str1 = "John told me, "
         + "\"Amelia said, 'It won't work anyway.'\"";
    System.out.println("String2: " + str1);
  }
}

Output:-

String1: “Java ‘Programming'”
String2: John told me, “Amelia said, ‘It won’t work anyway.'”

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.