Course – LS (cat=Java)

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

Hello World!” could be the first Java example we've seen when we started learning Java. We know if we pass a String object to the System.out.println() method, Java outputs the string in the console.

However, sometimes, we want the string to be quoted (” … “) in the output. In this quick tutorial, we'll explore how to achieve that.

2. Wrapping With Two Escaped Quote Strings

If we want to wrap a string in quotes (“…”), the most straightforward idea would be concatenating the quotes to the beginning and the end of the given text.

In Java, when we use a string value, we must quote it, for example, System.out.println(“Hello World!”);.However, we cannot put a quote character in a string like “””. Java doesn't accept it. Therefore, in this case, we must escape the quote symbol in a string: “\””.

Next, let's try it with an input example:

String theySay = "All Java programmers are cute!";
String quoted = "\"" + theySay + "\"";


When we run the program above, we can see the output with quotes:

"All Java programmers are cute!"

3. Verifying the Output in a Unit Test

Usually, we build unit tests to verify if a method work as expected. However, this case is a bit special, as we need to verify the output that we print to the console. To verify the output, we can replace the System.out with another PrintStream object which uses ByteArrayOutputStream as the OutputStream:

final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
final PrintStream originalOut = System.out;

void replaceOut() {
    System.setOut(new PrintStream(outContent));

void restoreOut() {

The @BeforeEach and @AfterEach are two annotations from JUnit 5. The annotated methods will be invoked before and after each test method execution.

Now, if we put our original output code in a test method, we can verify the printed output:

String theySay = "All Java programmers are cute!";
String quoted = "\"" + theySay + "\"";


String expected = "\"All Java programmers are cute!\"\n";
assertEquals(expected, outContent.toString());

The test passes if we give it a run. So, for simplicity, in later examples, we'll use unit test assertions to verify the output printed by System.out.

4. Using the replaceAll() Method

The standard String.replaceAll() method can perform string substitution operations by regex. We've solved the problem by concatenating a quote to the input string's beginning and end.

Following the same idea, we can implement it using the replaceAll() method:

String theyAsk = "Can you write Java code?";
String quoted = theyAsk.replaceAll("^|$","\"" );


String expected = "\"Can you write Java code?\"\n";
assertEquals(expected, outContent.toString());

The example above shows the regex “^|$” matches the input string's beginning and end. Therefore, the replaceAll() method replaces the matches with a quote.

5. Wrapping With Two Quote Characters

So far, we've learned two ways to print a string wrapped in quotes. Both approaches escape the quote character in a string like “\””. It does the job correctly. However, using escape characters can make code harder to read and understand.

To avoid escaping the quote character, we can use char instead of a string:

String weSay = "Yes, we can write beautiful Java codes!";
String quoted = '"' + weSay + '"';

String expected = "\"Yes, we can write beautiful Java codes!\"\n";
assertEquals(expected, outContent.toString());

If we run the test, it passes. As the test above shows, we perform the addition operation on two chars and a String: ‘”‘ + weSay + ‘”‘. This does the trick because Java automatically converts the char into a String, then concatenates it with the string weSay. Thus, no escaping is required.

6. Conclusion

In this article, we've explored three different approaches to printing a string with quotes () around it:

  • “\”” + input + “\”” – add an escaped quote string (“\””) to the beginning and end of the input string
  • input.replaceAll(“^|$”, “\””) – use the regex based replaceAll() method
  • ‘”‘ + input + ‘”‘ – similar to the first solution, but use char instead of String to avoid escaping

Apart from that, we've learned how to verify the output written by System.out.println() in a unit test.

As usual, all code snippets presented here are available over on GitHub.

Course – LS (cat=Java)

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Inline Feedbacks
View all comments