What is the Difference Between replace vs. replaceAll method in Java String Class?
Tags: article, blog, java, stringclass
Published: 2024-06-03 21:11:45

---
title: What is the Difference Between replace vs. replaceAll method in Java String Class?
published: true
date: 2023-10-27 07:20:47 UTC
tags: article,Blog,Java,StringClass
canonical_url: https://deniskisina.dev/java-stringreplace-vs-stringreplaceall-method/
---

* * *

Although the _replace()_ and _replaceAll()_ replace all occurrences in a string with a given character or string, they serve different purposes and work differently. Knowing how both methods work is essential to understand why one option is preferable.

In this article, I will explain the difference between replace vs. replaceAll methods in Java String class.

**Java String.replace() method: **

The `replace()` method is part of the Java String class introduced in SDK 1.5. The method creates a new String object where specified characters or substrings are replaced with new ones.

**_Note_** _: Java strings are immutable, meaning they cannot be changed. The `replace()` method creates a new string._

The method has two signatures:

1.  `replace(char oldChar, char newChar)`
2.  `replace(CharSequence target, CharSequence replacement)`

The first method replaces all occurrences of a specified `oldChar` with a `newChar`. In contrast, the second replaces all occurrences of a specified `target` CharSequence with a `replacement` CharSequence (_CharSequence is a superclass of String, meaning it accepts String as an argument_).

**Example 1**

Replace all occurrences of the character o with x.

`String originalString = "o1o2o3o4";`

`String newString = s.replace('o', 'x'); `

`// Original String: -> "o1o2o3o4"`

`// New String: -> "x1x2x3x4"`

**Example 2**

Replace all occurrences of the string red with black.

`String originalString = "The red car and the red house are both red.";`

`String newString = originalString.replace("red", "black");`

`// Original String: The red car and the red house are both red.`

`// New String: The black car and the black house are both black.`

**Java String.replaceAll() method: **

The method `replaceAll() `replaces each substring matching the given regex with the `replacement`.

**Signature:**

`public replaceAll(String regex, String replacement)`

**Example:**

`String originalString = "The red car and the red house are both reddish.";`

`// Replace all substrings matching the regex "red\\w*" with "black"`

`String newString = originalString.replaceAll("red\\w*", "black");`

`// Original String: The red car and the red house are both reddish.`

`// New String: The black car and the black house are both black.`

**Conclusion**

Starting from Java 9+, the `replace()` method was optimized to remove regex. However, the _`replaceAll()`_ method still calls the `java.util.regex.Pattern.compile()` method every time it is invoked, even if the first argument (_regex_) is not a regular expression. This leads to a significant overhead.

It is recommended to use _`replaceAll()`_ only when the first argument is a real regex; otherwise, use _`replace()`_, which does not have the performance drawback of regex.

.