Home >>Java String Methods >Java String replaceAll() method

Java String replaceAll() method

Java String replaceAll() method

Java replaceAll() method in java string is used to returns a string that replaces all the sequence of characters that match regex and replacement string.

Internal implementation

public String replaceAll(String regex, String replacement) 
{  
    return Pattern.compile(regex).matcher(this).replaceAll(replacement);  
}  

Syntax

public String replaceAll(String regex, String replacement)  

Parameters

regex - This parameter is used for regular expression

replacement - This parameter is used for replacement sequence of characters

Technical Details

Returns - It is used to return replaced string

Java String replaceAll() Method Example 1


public class ReplaceAllExample1
{  
public static void main(String args[])
{  
String n1="Education Website";  
String replaceString=n1.replaceAll("e","A"); 
System.out.println(replaceString);  
}
}  

Output:
Education WAbsitA

Java String replaceAll() Method Example 2


public class ReplaceAllExample2
{  
public static void main(String args[])
{  
String Name="Hello, how are you";  
String replaceString=Name.replaceAll("are","was");
System.out.println(replaceString);  
}
}  

Output:
Hello, how was you

No Sidebar ads