This article will discuss the usage and implementation of the “String replace()” method in Java.
What is the “String replace()” Method in Java?
The “replace()” method replaces each matching occurrence of the old character/substring in the string with the new character/substring.
Syntax
In the given syntax:
- “x” refers to the character or substring within the associated string that needs to be replaced.
- “y” corresponds to the newly replaced character or substring in the associated string.
Important Consideration: If the character or substring that needs to be replaced is not contained in the string, the “replace()” method returns the original character/string.
Example 1: Applying the “String replace()” Method to Replace a Character in Java
In this example, the “String replace()” method can be applied to replace the initialized character:
public static void main(String[] args) {
String character = "x";
System.out.println("The given character is: "+character);
System.out.println("The replaced first character is: "
+character.replace('x', 'a'));
System.out.println("The replaced second character is: "
+character.replace('y', 'b'));
}
}
According to the above code, apply the following steps:
- Firstly, initialize the character that needs to be replaced with a new one(character) and display it.
- After that, associate the “replace()” method with the initialized character as its parameters to replace the former specified character with the latter one.
- Lastly, apply the discussed method again such that the specified character(not contained) in the associated character becomes replaced.
- Note that the last step is applied to clear out the discussed “consideration” concept.
Output
In the above output, it can be observed that the last line yielded the “default character” since the specified character to be replaced can’t be found.
Example 2: Applying the “String replace()” Method to Replace a Substring in Java
In this particular example, the discussed method can be implemented to replace a “substring” within a string:
public static void main(String[] args) {
String string = "Java Programming";
System.out.println("The given string is: "+string);
System.out.println("The first replaced substring is: "
+string.replace("Java", "Linux"));
System.out.println("The second replaced substring is: "
+string.replace("Python", "Linux"));
}}
In this code block:
- First of all, initialize the string value and display it.
- Now, apply the “replace()” method by referring to the defined string and replacing the accumulated substring, as its former parameter with the latter parameter.
- Finally, repeat the discussed approach for replacing the substring(not found), i.e., “Python”.
Output
In this outcome, it can be seen that the specified substring is replaced properly.
Conclusion
The “String replace()” method in Java replaces each of the matching occurrences of the old character/substring in the string with the new character/substring. This method can be applied to replace the formerly specified character/substring with the latter one provided that it is valid or included in the string. This blog elaborated on the implementation of the Java “String replace()” method.