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 of String replace() in Java
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 Java String replace()
1. Example Java String replace(char old, char new)
Below is the implementation of the above-mentioned method:
Java
public class rep1 {
public static void main(String args[])
{
String Str = new String( "Welcome to geeksforgeeks" );
System.out.print( "After replacing all o with T : " );
System.out.println(Str.replace( 'o' , 'T' ));
System.out.print( "After replacing all e with D : " );
System.out.println(Str.replace( 'e' , 'D' ));
}
}
|
Output
After replacing all o with T : WelcTme tT geeksfTrgeeks
After replacing all e with D : WDlcomD to gDDksforgDDks
2. Example Java String replace(String target, String replacement)
We can implement replace() method with CharSequence just like with char.
Below is the implementation of the above method:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
String s1 = "GeeksforGeeks" ;
System.out.println(s1);
String replaceString = s1.replace( "Geeks" , "GfG " );
System.out.println(replaceString);
}
}
|
Output
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
import java.io.*;
class GFG {
public static void main(String[] args)
{
String str = "GeeksforGeeks" ;
int size = str.length();
System.out.println(str);
String target = null ;
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)
Java 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 of Java String replaceFirst()
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 that would replace the found expression.
Return Value
- This method returns a resulting String.
Example of Java String replaceFirst()
Below is the implementation of the above topic:
Java
public class rep3 {
public static void main(String args[])
{
String Str = new String( "Welcome to geeksforgeeks" );
System.out.print( "Original String : " );
System.out.println(Str);
System.out.print(
"After replacing 1st occurrence of regex with replace_str : " );
System.out.println(
Str.replaceFirst( "geeks" , "ASTHA" ));
}
}
|
Output
Original String : Welcome to geeksforgeeks
After replacing 1st occurrence of regex with replace_str : Welcome to ASTHAforgeeks
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Jul, 2023
Like Article
Save Article