String class in Java provides several methods to replace characters, CharSequence and substring from String in Java. Since String
is immutable in Java, every time you performance an operation on String
either replacement or removing
white space from String, it generates a new String object. There are four
overloaded method to replace String in Java :
Out of these, 2nd one which takes a CharSequence is added
on Java 1.5. CharSequence is actually a super interface for
String,
StringBuffer and StringBuilder in Java, which means you can pass any of String, StringBuffer or StringBuilder Object as
argument to this replace method. replaceFirst() and replaceAll() are very
powerful and accepts regular expression. replaceFirst() only
replace first match, while replaceAll replaces
all matches with replacement String provided.