Please note, this is a STATIC archive of website www.w3schools.com from 05 May 2020, cach3.com does not collect or store any user information, there is no "phishing" involved.
THE WORLD'S LARGEST WEB DEVELOPER SITE

Java String replace() Method

❮ String Methods


Example

Return a new string where all "l" characters are replaced with "p" characters:

public class MyClass {
  public static void main(String[] args) {
    String myStr = "Hello";
    System.out.println(myStr.replace('l', 'p'));
  }
}

Run example »


Definition and Usage

The replace() method searches a string for a specified character, and returns a new string where the specified character(s) are replaced.


Syntax

public String replace(char searchChar, char newChar)

Parameter Values

Parameter Description
searchChar A char, representing the character that will be replaced by the new character
newChar A char, representing the character to replace the searchChar with

Technical Details

Returns: A new String, where the specified character has been replaced by the new character(s)

❮ String Methods