Java String replaceAll() method example

The Java String replaceAll() returns a string after it replaces each substring of that matches the given regular expression with the given replacement.

1. String replaceAll() method

Use String.replaceAll(String regex, String replacement) to replace all occurrences of a substring (matching argument regex) with replacement string.

1.1. Method syntax

/**
* @param regex - regular expression to match in given string
* @param replacement : replacement string to be replaced
* 
* @return result string after replacing all occurrence of 
* matching 'regex' with replacement 'substring'
*/
public String replaceAll(String regex, String replacement) 
{
	return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

1.2. throws PatternSyntaxException

Watch out that replaceAll() throws PatternSyntaxException if the regular expression’s syntax is invalid. In given example, "[" is invalid regular expression.

public class StringExample 
{
    public static void main(String[] args) 
    {       
        String newStr = "hello world".replaceAll("[", "");
    }
}

Program output.

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0
[
^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.clazz(Pattern.java:2548)
	at java.util.regex.Pattern.sequence(Pattern.java:2063)
	at java.util.regex.Pattern.expr(Pattern.java:1996)
	at java.util.regex.Pattern.compile(Pattern.java:1696)
	at java.util.regex.Pattern.<init>(Pattern.java:1351)
	at java.util.regex.Pattern.compile(Pattern.java:1028)
	at java.lang.String.replaceAll(String.java:2210)
	at com.StringExample.main(StringExample.java:9)

2. Java String replaceAll() example

2.1. Replace all occurrences of a substring or word

Java program to replace all occurrences of word in a string. In this example, we are replacing word “java” with “scala”.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String str = "how to do in java provides java tutorials";
        
        String newStr = str.replaceAll("java", "scala");
        
        System.out.println(newStr);
    }
}

Program output.

how to do in scala provides scala tutorials

2.2. Replace all white spaces

Java program to replace all occurrences of white spaces in a string.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String str = "how to do in java provides java tutorials";
        
        String newStr = str.replaceAll("\\s", "");
        
        System.out.println(newStr);
    }
}

Program output.

howtodoinjavaprovidesjavatutorials

References:

A Guide to Java String
String Java Doc

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

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.

Our Blogs

REST API Tutorial