Java - String replace() Method
The Java string replace() method returns the string where the specified character is replaced with new character in the given string.
Syntax
public String replace(char searchChar, char newChar)
Parameters
searchChar |
specify the character to be replaced. |
newChar |
specify the character to replace with. |
Return Value
Returns the replaced version of the specified string.
Exception
NA.
Example:
In the example below, replace() method returns the string where the character 'l' is replaced with 'k' in the given string.
import java.lang.*; public class MyClass { public static void main(String[] args) { String MyString = "Hello World!"; String NewString = MyString.replace('l', 'k'); System.out.println(NewString); } }
The output of the above code will be:
Hekko Workd!
❮ Java.lang - String