Java String replaceAll() Method

The String.replaceAll() method in Java is used to replace each substring of a string that matches a given regular expression with a specified replacement string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. replaceAll Method Syntax
  3. Examples
    • Replacing Substrings Using a Regular Expression
    • Replacing Digits with a String
    • Handling Special Characters
  4. Conclusion

Introduction

The String.replaceAll() method is a member of the String class in Java. It allows you to replace all substrings of a string that match a given regular expression with a specified replacement string. This method is particularly useful for text processing and data cleaning tasks that require pattern-based replacements.

replaceAll Method Syntax

The syntax for the replaceAll method is as follows:

public String replaceAll(String regex, String replacement)
  • regex: The regular expression to match substrings to be replaced.
  • replacement: The string to replace each matched substring.

Examples

Replacing Substrings Using a Regular Expression

The replaceAll method can be used to replace all occurrences of a pattern in a string with a specified replacement string.

Example

public class ReplaceAllExample {
    public static void main(String[] args) {
        String message = "The rain in Spain stays mainly in the plain.";

        String newMessage = message.replaceAll("ain", "ane");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: The rain in Spain stays mainly in the plain.
New message: The rane in Spane stays manely in the plane.

Replacing Digits with a String

The replaceAll method can be used to replace all digits in a string with a specified replacement string.

Example

public class ReplaceAllExample {
    public static void main(String[] args) {
        String message = "Order number: 12345";

        String newMessage = message.replaceAll("\\d", "#");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: Order number: 12345
New message: Order number: #####

Handling Special Characters

The replaceAll method can handle special characters in regular expressions by using escape sequences.

Example

public class ReplaceAllExample {
    public static void main(String[] args) {
        String message = "Price: $10.00";

        String newMessage = message.replaceAll("\\$", "USD ");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: Price: $10.00
New message: Price: USD 10.00

Removing Whitespace

The replaceAll method can be used to remove all whitespace from a string.

Example

public class ReplaceAllExample {
    public static void main(String[] args) {
        String message = "Java is fun";

        String newMessage = message.replaceAll("\\s", "");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: Java is fun
New message: Javaisfun

Conclusion

The String.replaceAll() method in Java is used for replacing substrings based on regular expressions. By understanding how to use this method, you can efficiently perform pattern-based replacements in your Java applications. Whether you are replacing specific substrings, digits, special characters, or whitespace, the replaceAll method provides a reliable solution for these tasks.

Comments