Advertisement



< Prev
Next >



replace() method





As per the name of the method goes, replace() method of String class replaces all the occurrences of a particular character with a new character in a String object.




Signature of replace() method


public String replace(char oldChar, char newChar)






Note :


If the character to be replaced in a String is not found then String returned by the replace() method is the same as the String that invoked replace() method.




Calling replace() method on a String object.


// Program to replace a particular character with different character in a String.

class StringReplace
{
public static void main(String[] ar)
{
String str1= new String("Java Rules");

System.out.println("Original String is : " + str1);

System.out.println("String's value after replacing a with x : " + str1.replace('a', 'x'));
}
} 


Output -


Original String is : Java Rules
String's value after replacing a with x : Jxvx Rules


Program Analysis


Here in the next example, we have initialized a String with a value Java Rules and we are calling on replace() method to replace all occurrences of char value 'a' with 'x'.





Method replace() does not modify the content of invoked String object


The method replace() only returns the content of String object after replacing a character in it with a new character, without modifying its original content..
// Program to replace a particular character with different character in a String.

class StringReplace2
{
public static void main(String[] ar)
{
String str1= new String("Never give up!");

System.out.println("Original String is : "+str1);

System.out.println("String's value after replacing e with E  : " + str1.replace('e', 'E'));

System.out.println("String's value after replace() is called : " + str1);
}
} 


Output -


Original String is : Never give up!
String's value after replacing a with x : NEvEr givE up!
String value after replace() is called  : Never give up!


Program Analysis


We have initialized a String object with value Never give up! Post calling replace('e', 'E') method, the String object still has the same value Never give up! because replace() method doesn't modify the invoked String object.


Advertisement




Strings are immutable but reference variables are mutable.


String objects are immutable, but the reference variables pointing to them are not, therefore in order to display the result of calling the replace() method on the original object, we can make the reference variable to the original string, point to the result of replacing a character with another returned by the replace() method(but in this case, the original string will be lost).

// Program to modify the invoked String object with the result of replace() method.

class StringReplace3
{
public static void main(String[] ar)
{
String str1= new String("Java Rules");

System.out.println("Original String is : "+str1);

str1 = str1.replace('a', 'x');
System.out.println("String's value after replacing a with x : " + str1);
}
}


Output is :



Original String is : Java Rules
String's value after replacing a with x : Jxvx Rules


Program Analysis






Please share this article -





< Prev
Next >
< split() method
equals() method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement