JavaStringreplace

Replaces all occurrences of a character or string.

Syntax

str.replace(old, new)

Example

Enter values below to update the example in real time.

String
System
out
println
String s = "Hello World";
System.out.println(s.replace('o', '0'));          // "Hell0 W0rld"
System.out.println(s.replace("World", "Java"));  // "Hello Java"