Java Program to Replace Multiple Characters in a String
Last Updated :
06 Jun, 2021
In this program, we will be discussing various methods for replacing multiple characters in String. This can be done using the methods listed below:
- Using String.replace() method
- Using replaceAll() method
- Using replaceFirst() method
Method 1: Using String.replace() method
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:
- The old character.
- The new character.
Return Value: It returns a string derived from this string by replacing every occurrence of old character with a new character.
Example
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
Method 2: Using replaceAll() method
This method replaces each substring of the string that matches the given regular expression with the given replace_str.
Syntax:
public String replaceAll(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 the resulting String.
Example
Java
public class rep2 {
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 regex with replace_str : " );
System.out.println(Str.replaceAll( "(.*)geeks(.*)" , "AKSHIT SAXENA" ));
}
}
|
Output
Original String : Welcome to geeksforgeeks
After replacing regex with replace_str : AKSHIT SAXENA
Method 3: Using replaceFirst() 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.
Example
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" , "Akshit" ));
}
}
|
Output
Original String : Welcome to geeksforgeeks
After replacing 1st occurrence of regex with replace_str : Welcome to Akshitforgeeks
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...