The method replace() replaces all occurrences of a String in another String or all occurrences of a char with another char.

Available Signatures

public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)


void whenReplaceOldCharNewChar_thenCorrect() {
    String s = "wslcoms to basldung";

    assertEquals("welcome to baeldung", s.replace('s', 'e'));
void whenReplaceTargetReplacement_thenCorrect() {
    String s = "welcome at baeldung, login at your course";

    assertEquals("welcome to baeldung, login to your course", s.replace("at", "to"));