What is the difference between replace and replaceAll in Strings? Please explain it with an example. | Selenium Forum
R
Romit Posted on 05/07/2022

I have created a basic program, but the output is the same, so what is the diff between the?

 

public class Main
{
public static void main(String[] args) {
String str1 ="Hello is Hello";
System.out.println(str1.replace("Hello", "Good"));
System.out.println(str1.replaceAll("Hello", "Good"));
}
}


A
Ashish Thakur Replied on 12/07/2022

str1="ABCD EFGH ABCD"

System.out.println(str1.replace("ABCD", "XYZ")); -> XYZ EFGH ABCD

System.out.println(str1.replaceAll("ABCD", "XYZ")); -> XYZ EFGH XYZ