Java String replaceAll() replaces all the occurrences of a particular character, string or a regular expression in the string. The method returns a new string as its output. The method requires two parameters.

String replaceAll() Syntax

public String replaceAll(String regex, String replacement)

regex that represents the pattern to look for while replacing and a replacement **string **to replace it with.

There are multiple types of substitutions that are possible with String replaceAll() method.

Replacing a single character

To replace a single character, specify the character to be replaced as the regex.

public class Main {
 
    public static void main(String[] args) {
        String s = "Welcome to JournalDev";
        System.out.println("Original String : "+s);
        System.out.println();
        String rep = s.replaceAll("o","@");
        System.out.println("New String: "+rep);
    }
}

#string #java #replaceall()

Java String replaceAll() Method
2.65 GEEK