Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java String replaceAll()

The String.replaceAll(regex, replacement) in Java replaces all occurrences of a substring matching the specified regular expression with the replacement string. A similar method replace(substring, replacement) is used for matching the literal strings, while replaceAll() matches the regex. Note that in regular expressions, literal strings are also patterns, so …

Lokesh Gupta

January 6, 2023

Java String class
Java String
Java String

The String.replaceAll(regex, replacement) in Java replaces all occurrences of a substring matching the specified regular expression with the replacement string.

A similar method replace(substring, replacement) is used for matching the literal strings, while replaceAll() matches the regex. Note that in regular expressions, literal strings are also patterns, so replaceAll() will work with literal strings as well, in addition to regex patterns.

1. String.replaceAll() API

The syntax of the replaceAll() API is as follows:

String updatedString = thisString.replaceAll(regex, replacement);
  • thisString: the string that should be searched for substring pattern and modified.
  • regex: pattern to be searched.
  • replacement: each occurrence of the matches will be replaced with this substring.
  • updatedString: the result of the API that is the modified string.

2. String.replaceAll() Example

The following Java program demonstrates the usage of replaceAll() API.

2.1. Replace All Occurrences of a Word

The following Java program replaces all occurrences of “java” with “scala“.

String str = "how to do in java !! a java blog !!";

Assertions.assertEquals("how to do in scala !! a scala blog !!", str.replaceAll("java", "scala"));

2.2. Remove All Whitespaces

The following Java program replaces all occurrences of whitespaces in a string with an empty string.

String blog = "how to do in java";

Assertions.assertEquals("howtodoinjava", blog.replaceAll("\\s", ""));

3. PatternSyntaxException

We should know that replaceAll() throws PatternSyntaxException if the regex’s syntax is NOT valid. In the given example, “[” is an invalid regular expression, so we get the exception in runtime.

Assertions.assertThrows(PatternSyntaxException.class, () -> {
  blog.replaceAll("[", "");
});

Happy Learning !!

References: String Java Doc

Sourcecode on Github

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

String Methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

Table of Contents

  • 1. String.replaceAll() API
  • 2. String.replaceAll() Example
    • 2.1. Replace All Occurrences of a Word
    • 2.2. Remove All Whitespaces
  • 3. PatternSyntaxException
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java String replaceFirst()

Next

Java String split() : Splitting by One or Multiple Delimiters

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap