Course – LS – All

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

>> CHECK OUT THE COURSE

1. Overview

In Java, single quotes are used to define char literals, and double quotes define String literals. We can also use single quotes in String literals. In this tutorial, we’ll learn how to replace single quotes in Java String.

2. How to Escape Special Characters in Java?

A String is a sequence of characters. These characters can be letters, numbers, punctuation marks, etc. When creating a String, it must be enclosed in double quotes, but what should we do if we need to create a String that must contain single quotes itself? Well, Java will misunderstand the String and throw an error because a single quote is interpreted as a special character.

To solve this problem, we can simply use one of the escaping characters, such as the special backslash \ character that turns special characters into String characters.

For instance, let’s consider the case where we want to replace the single quotes in a String with \’.

3. Using String.replace()

Let’s use the String.replace(CharSequence target, CharSequence replacement) method to do the String replacement. This method replaces all occurrences of the target with replacement.

Let’s see how to use the String.replace() method to replace single quotes in Java String:

String ORIGINAL_STRING = "This is 'Baeldung' tutorial.";
String EXPECTED_STRING = "This is \\'Baeldung\\' tutorial.";

@Test
public void givenString_thenReplaceUsinReplaceMethod() {
    String modifiedString = ORIGINAL_STRING.replace("'", "\\'");
    assertEquals(EXPECTED_STRING, modifiedString);
}

In the above example, we used the value \\’ as a replacement parameter in the String.replace() method. The first backslash escapes the second one, and together with the single quote, it forms the escaped sequence \’. In this simple way, we replaced the single quotes in the desired String with \’.

4. Using String.replaceAll()

The String.replaceAll(String regex, String replacement) method is similar to the String.replace(CharSequence target, CharSequence replacement). The main difference between them is in how they handle the substrings to replace. Furthermore, replace() works with plain text and replaces literal occurrences, while replaceAll() works with regular expressions.

For demonstration purposes, we’ll use the same example as the previous section:

@Test
public void givenString_thenReplaceUsinReplaceAllMethod() {
    String modifiedString = ORIGINAL_STRING.replaceAll("'", "\\\\'");
    assertEquals(EXPECTED_STRING, modifiedString);
}

In the above example, we used the value \\\\’ as a replacement parameter in the String.replaceAll() method. The first two backslashes are used to correctly escape the backslash character in the regular expression, and then \\’ represents the escaped single quote character \’.

Remember that Strings in Java are immutable, meaning that the replace() or replaceAll() methods don’t modify the original String but instead return a new modified String. So, make sure to assign the result of the method call to a new variable.

5. Conclusion

In this short article, we learned how to replace single quotes in Java String. The source code is available over on GitHub.

Course – LS – All

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

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.