Java String replace()

The replace() method has two forms. The first one replaces all occurrences of one character in the invoking string with another character. It has the following simple form:

String replace(char org, char rep)

Here the original character will be replaced by the character specified by the replacement. The resulting string returns. For example,

String str = "Hello".replace('o','p');

puts the string "Hellp" into str.

The second form of replace() replaces one character sequence with another. It has this general form:

String replace(CharSequence original, CharSequence replace)


// String replacement
class Replacement
 String _firstName;
 String _lastName;
 Replacement(String firstName,String lastName)
 void PrintFullName(String replaceLastname)
  System.out.println("Full Name : " + this._firstName+" "+this._lastName);
  System.out.println("After replacement");
  System.out.println("Full Name : "+this._firstName+" "+this._lastName.replace(_lastName,replaceLastname));
public class Main 
  public static void main(String[] args) 
    Replacement c1=new Replacement("chandan","payra");