Java.lang.string.replace() method in Java
Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are also a type of exceptional array that holds characters, therefore, strings are immutable as well.
The String class of Java comprises a lot of methods to execute various operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), substring(), etc. Out of these methods, we will be focusing on the Java replace() method.
Java String replace()
This method returns a new string resulting from replacing all occurrences of old characters in the string with new characters.
Syntax:
public String replace(char oldch, char newch)
Parameters:
oldch: the old character. newch: the new character.
Return Value: It returns a string derived from this string by replacing every occurrence of oldch with newch.
Examples of String replace()
Example 1:
Java
// Java code to demonstrate the // working of replace() public class rep1 { public static void main(String args[]) { // Initialising String String Str = new String( "Welcome to geeksforgeeks" ); // Using replace to replace characters System.out.print( "After replacing all o with T : " ); System.out.println(Str.replace( 'o' , 'T' )); // Using replace to replace characters System.out.print( "After replacing all e with D : " ); System.out.println(Str.replace( 'e' , 'D' )); } } |
After replacing all o with T : WelcTme tT geeksfTrgeeks After replacing all e with D : WDlcomD to gDDksforgDDks
Note: We can implement replace() method with CharSequence just like with char.
Example 2:
Java
// Java Program to implement // replace() method import java.io.*; class GFG { public static void main(String[] args) { String s1 = "GeeksforGeeks" ; // orignal string System.out.println(s1); // Replace Geeks with Gfg String replaceString = s1.replace( "Geeks" , "GfG " ); // New String System.out.println(replaceString); } } |
GeeksforGeeks GfG forGfG
Exceptions with replace() method
1. NullPointerException
The null regular expression is not accepted by the replace() method, it raises the NullPointerException.
Java
// Java Program to implement // Java replaceAll() method import java.io.*; // Driver Class class GFG { // Main function public static void main(String[] args) { String str = "GeeksforGeeks" ; int size = str.length(); System.out.println(str); String target = null ; // replacing null with GFG str = str.replace(target, "GFG" ); System.out.println(str); } } |
Output
Exception in thread "main" java.lang.NullPointerException at java.base/java.lang.String.replace(String.java:2142) at GFG.main(GFG.java:12)
String replaceFirst():
Just like replace() method this method replaces the first substring of this string that matches the given regular expression with the given replace_str.
Syntax:
public String replaceFirst(String regex, String replace_str)
Parameters:
regex: the regular expression to which this string is to be matched. replace_str: the string which would replace found expression.
Return Value: This method returns a resulting String.
Below is the implementation of the above topic:
Java
// Java code to demonstrate the // working of replaceFirst() public class rep3 { public static void main(String args[]) { // Initialising String String Str = new String( "Welcome to geeksforgeeks" ); // original string System.out.print( "Original String : " ); System.out.println(Str); // Using replaceFirst to replace regex with // replace_str Replaces 1st occurrence of geeks with // ASTHA System.out.print( "After replacing 1st occurrence of regex with replace_str : " ); System.out.println( Str.replaceFirst( "geeks" , "ASTHA" )); } } |
Original String : Welcome to geeksforgeeks After replacing 1st occurrence of regex with replace_str : Welcome to ASTHAforgeeks
This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Please Login to comment...