Public String replace (char oldChar, char newChar)
Returns a new string, which is obtained by replacing all the oldChar in the string with newChar.
If oldChar does not appear in the character sequence represented by this String object, a reference to this String object is returned.
Otherwise, create a new String object, which represents the character sequence except that all oldChar is replaced with newChar, which is the same as the character sequence represented by this String object.
Public class MainClass
{
Public static void main (String args [])
{
String s1 = new String ("hello ");
String s2 = new String ("GOODBYE ");
String s3 = new String ("spaces ");
System. out. printf ("s1 = % sns2 = % sns3 = % snn", s1, s2, s3 );
// Test method replace
System. out. printf ("Replace 'L' with 'l' in s1: % snn", s1.replace ('L', 'L '));
} // End main
}
For special nr, we replace it with br.
Public class Main {
Public static void main (String [] argv ){
System. out. println ("rn | r | n | nr". replaceAll ("(rn | r | n | nr)", "<br> "));
}
}
// <Br> | <br>