How To

How to add double quotes around java object and string variable

add double quotes around java object and string

Learn how to add double quotes around java object and string variables by escaping characters in this two-minute guide. In every programming language single-quotes (‘ ’) and double-quotes (“ ”) are used for string variables initialization. Therefore, whenever you use these two symbols around a text, Java, javascript, c# and all other languages will treat such text as a string even if it’s a number surrounded with a single or double-quotes.

So, if you have ever coded a project that you needed to add double quotes around a string, you can’t do it as you would in string variables initialization. Below is an example of what you might try to do.

public class Main {

 public static void main(String args[]) {	 
	 System.out.println("" Helloo world "");
 }
 
}
/* OUTPUT:
	
	Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
		The left-hand side of an assignment must be a variable
		Syntax error on token "hellll", invalid AssignmentOperator

		at myjavaapp.Main.main(Main.java:8)

*/

You can see that the Java virtual machine (JVM) couldn’t compile the above code. Why because it doesn’t understand what we are trying to print on the console. We have two double-quotes surrounded with the text we want to log to the console. The main problem is that the compiler can’t figure out what data-type such value should be compiled to.

Check also, How to validate an email address in JavaScript

How do you add double quotes around java object and string variables easily?

Let me show you an example of how to do it in few seconds…

How do you add double quotes to a string variable in java?

add double quotes around java object and string

You can add double quotes to a string variable by using escape character symbol “\”. Using this symbol will notify the JVM to ignore the extra quotes around the text. All that you need to do is to add “\” in-front of the text and \”” at the end.

Example, add double quotes to a String variable in java using escape characters.

public class Main {

 public static void main(String args[]) {
	 // Escape the needed double-quotes in the string
	 System.out.println("\"Helloo world\"");
 }
 
}
/* OUTPUT:
	
"Helloo world"

*/

But what if you want to enclose or puts double-quotes around a certain part of string variable? For example, let say you want to display a story of 100 text length and part of it needs to be enclose in a quote, how can you achieve it? Java strings have a specific method called the replace() that you can use to manipulate your strings together with escape characters. Its easy, let me show you an example…

public class Main {

 public static void main(String args[]) {
	 // imagine we have this string and we want to surround the languages with quotes
	    String me = "I love computer progamming languages like java and c#";
	    // use the replace() method to replace part of the string
	    me = me.replace("java", "\"java\"").replace("c#", "\"c#\"");
	    System.out.println(me);
 }
 
}
/* OUTPUT:
I love computer progamming languages like "java" and "c#"
*/ 

The string replace() will look through the string value and replace the matching text with the second arguments specified in the method. And since the value specified as a second argument does escape quotes, we are able to achieve the output we want.

Check also, How to think like a programmer for effective problem solving?

How do you concatenate double quotes in Java?

You can concatenate double quotes in java by using the addition + operator. This operator is used for performing addition of numbers and concatenation of strings. We can use it to concatenate other quotes since we can escape sequence and join them together using the + operator. When I say concatenation, it simply means joining two or more values together.

Double quotes concatenation example in Java

public class Main {

 public static void main(String args[]) {
	 // a string with quote: i know "Java" 
	 String languages = "i know \"Java\" ";
	 // Concatenate double quotes to it
	 System.out.println(languages + "and " +  "\"c#\"");
 }
 
}
/* OUTPUT:
	
i know "Java" and "c#"

*/

How do you remove double quotes from a string in Java?

You can use the replace() method to remove double quotes from a string variable.

Example

public class Main {

 public static void main(String args[]) {
	 // a string with quote: i know "Java" 
	 String languages = "i know \"Java\"";
     languages = languages.replace("\"", "");
	 System.out.println(languages);
 }
}
/* OUTPUT:
i know Java 
*/

Check also: Java error fixed: Cannot make a static reference to the non-static method or fields

How to split string with double quotes in java?

How do you split a string with double quotes? The splite() method returns an array of certain values based on the condition, but in our case it should be a string. You can use this method to query a string variable and split the double quotes in it to an array.

Example: Split string with double quotes in java

public class Main {

 public static void main(String args[]) {
	 String test = "Go to school at \"2022-10-04 03:30:00.0\"";
     // grab each text before the \ symbol 
     String parts[] = test.split("\"");
     String part0 = parts[0];
     String part1 = parts[1];
     System.out.println(part0);
     System.out.println(part1);
 }
}
/* OUTPUT:
Go to school at 
2022-10-04 03:30:00.0
*/

That’s all, you can improve your Java string manipulation skills by searching for solution to challenges like:

  • How to append single quotes to list of string in java?
  • how to put double quotes in json string java?
Tagged

About Justice Ankomah

computer science certified technical instructor. Who is interested in sharing (Expert Advice Only)
View all posts by Justice Ankomah →

Leave a Reply

Your email address will not be published. Required fields are marked *