Home >>Java String Methods >Java String replace() Method
Java replace() method in Java String is used to searches a string for a specified character, and returns a new string where the specified character are replaced.
public String replace(char searchChar, char newChar)
searchChar - This parameter is used to represents A char the character that will be replaced by the new character
newChar - This parameter is used to represents A char the character to replace the searchChar with
Returns - It is used to return A new String, where the specified character has been replaced by the new character
public class ReplaceExample1
{
public static void main(String[] args)
{
String Name = "php";
System.out.println(Name.replace('p', 'h'));
}
}
public class ReplaceExample2
{
public static void main(String[] args)
{
String str1 = "kkkkk-lllll-mmmmm";
String rs = str1.replace("k","a");
System.out.println(rs);
rs = rs.replace("m","c");
System.out.println(rs);
}
}