< 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.




Modifying the String object by replacing a character in it with another


In the upcoming code, we are modifying the content of the String object on which replace() method is called, by storing the result of replacing a character with another, back in the same String object.
// 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("Original String's value after replacing a with x : " + str1);
}
}


Output is :



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


Program Analysis


The original content of String object is modified by placing the result of replace() method back in the same String object.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< split() method
equals() method >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2