Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1 Comment
Oldest
Newest