Programming
Java
String Manipulation
Coding Techniques
Code Comparison

Difference between String replace() and replaceAll()

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Java, replace() and replaceAll() can both change text inside a string, but they do not interpret the search pattern the same way. replace() performs literal replacement, while replaceAll() treats the first argument as a regular expression.

Use replace() for Literal Text

replace() is the right choice when you want to replace an exact character or substring and do not need regex rules.

java
1String message = "hello world";
2
3System.out.println(message.replace('l', 'p'));
4System.out.println(message.replace("world", "team"));

Because the target is treated literally, characters such as . or * have no special meaning here.

java
String value = "1.2.3";
System.out.println(value.replace(".", "-"));

That replaces every literal dot. No escaping is required.

Use replaceAll() for Regex Patterns

replaceAll() uses a regular expression for the first argument, which makes it much more powerful and more dangerous if you only wanted a plain literal.

java
String message = "hello   world";
System.out.println(message.replaceAll("\\s+", " "));

This is useful when the pattern matters, such as:

  • collapsing repeated whitespace
  • replacing all digits
  • removing punctuation classes

For example:

java
String value = "Order-12345";
System.out.println(value.replaceAll("\\d", "#"));

That replaces every digit because \\d is a regex token, not a literal string.

Watch Out for Special Characters

A common bug is using replaceAll() when the pattern contains regex metacharacters that should have been treated literally.

java
String value = "1.2.3";
System.out.println(value.replaceAll(".", "-"));

This does not mean "replace dots". In regex, . means "any character", so the result is very different from what many developers expect.

If you want literal replacement, use replace(). If you truly need replaceAll() with a literal regex input, escape it carefully or use Pattern.quote(...).

There is also a third related method worth knowing: replaceFirst(). It behaves like replaceAll() in that it uses regex syntax, but it changes only the first matching occurrence. That is useful when the pattern is regex-based but the change should happen once.

Another practical example makes the distinction clearer:

java
1String text = "abc123def456";
2
3System.out.println(text.replace("123", "-"));
4System.out.println(text.replaceAll("\\d+", "-"));

The first call replaces one literal substring. The second uses a regex to replace every run of digits, which is a very different capability even though both methods look similar at a glance.

That difference becomes especially important when user input or configuration text is involved. If the search text comes from outside the code and you do not intend regex semantics, replace() is usually the safer default because it avoids accidental pattern interpretation completely in production code paths everywhere safely always.

Common Pitfalls

The biggest mistake is choosing replaceAll() for simple literal substitutions. That adds regex overhead and can produce surprising results when the pattern contains special characters.

Another common issue is forgetting that the replacement string in regex-based methods can also treat certain sequences specially. If the replacement contains backslashes or dollar signs, you may need to escape them.

People also forget that replaceAll() is not the only regex variant. replaceFirst() exists when you want regex replacement on only the first match.

Finally, if performance matters and the task is a simple literal replacement, replace() is usually the cleaner and cheaper tool.

Summary

  • 'replace() performs literal character or substring replacement.'
  • 'replaceAll() treats the search pattern as a regular expression.'
  • Use replace() for plain text substitutions.
  • Use replaceAll() when you actually need regex pattern matching.
  • Be careful with metacharacters such as . and *, because they change meaning under regex rules.

Course illustration
Course illustration

All Rights Reserved.