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.
public String replaceAll(String regex, String replacement)
A 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.
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()