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
// 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'));
}
}
OutputAfter 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
// Java code to demonstrate the
// working of replaceAll()
public class rep2 {
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 replaceAll to replace regex with replace_str
System.out.print("After replacing regex with replace_str : " );
System.out.println(Str.replaceAll("(.*)geeks(.*)", "AKSHIT SAXENA"));
}
}
OutputOriginal 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
// 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", "Akshit"));
}
}
OutputOriginal String : Welcome to geeksforgeeks
After replacing 1st occurrence of regex with replace_str : Welcome to Akshitforgeeks
Similar Reads
Java Program to Swap characters in a String The task at hand involves manipulating a string S of length N, given as input. The string is subjected to a series of B swaps, each performed according to the following procedure: The character at position i is swapped with the character located C positions ahead of it, or (i + C)%N. This swapping p
6 min read
Java Program to Replace All Line Breaks from Strings Given a string, write a Java program to replace all line breaks from the given String. Examples Input - Geeks For Geeks Output - Geeks For Geeks Line Break: A line break ("\n") is a single character that defines the line change. In order to replace all line breaks from strings replace() function can
2 min read
Java Program to Add Characters to a String We will be discussing out how to add character to a string at particular position in a string in java. It can be interpreted as follows as depicted in the illustration what we are trying to do which is as follows: Illustration: Input: Input custom string = HelloOutput: --> String to be added 'Gee
4 min read
Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided
2 min read
Remove first and last character of a string in Java Given a string str, the task is to write the Java Program to remove the first and the last character of the string and print the modified string.Examples:Input: str = "GeeksForGeeks"Output: "eeksForGeek"Explanation: The first and last characters of the given string are 'G' and 's' respectively. Afte
4 min read
Java program to swap first and last characters of words in a sentence Write a Java Program to Swap first and last character of words in a Sentence as mentioned in the example? Examples: Input : geeks for geeks Output :seekg rof seekg Approach:As mentioned in the example we have to replace first and last character of word and keep rest of the alphabets as it is. First
2 min read
Convert String to Stream of Chars in Java The StringReader class from the java.io package in Java can be used to convert a String to a character stream. When you need to read characters from a string as though it were an input stream, the StringReader class can be helpful in creating a character stream from a string. In this article, we wil
2 min read
How to Replace All Occurings of String Using Regex in Java? Regex in Java is an interesting way to search for patterns in a string that the user provides. Expanded as Regular Expressions, It consists of some patterns that can be planned and modified according to the usage in the program. Example of Replace All Occurings of a StringInput: str="This is a sampl
2 min read
Remove all non-alphabetical characters of a String in Java Given a string str, consisting of non-alphabetical characters. The task is to remove all those non-alphabetical characters of str and print the words on a new line. Examples: Input: str = "Hello, how are you ?" Output: Hello how are you comma(, ), white space and question mark (?) are removed and th
4 min read
How to remove all non-alphanumeric characters from a string in Java Given string str, the task is to remove all non-alphanumeric characters from it and print the modified it. Examples: Input: @!Geeks-for'Geeks,123 Output: GeeksforGeeks123 Explanation: at symbol(@), exclamation point(!), dash(-), apostrophes('), and commas(, ) are removed.Input: Geeks_for$ Geeks?{}[]
3 min read