Program to find and replace characters on string in java
In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method.
String Replace
It returns a string replacing all old characters with new characters or sequences.
Syntax:
String replace(previous char character, new character)
Using this method, you can change the similar characters of the String with a new character. Both old and character are passed as parameters to the replace method.
Return Value: It returns a string with old characters replaced by new characters, passed as parameters to the replace method.
Program for Replace method for strings in Java
//Replace method in Java
public class Main {
public static void main(String args[])
{
// Declaration of string
String s = new String("Hello everyone");
// Before replacing characters
System.out.print("String before replacing : ");
System.out.println(s);
System.out.print("After replacing all e with x : ");
System.out.println(s.replace('e', 'x'));
// Using replace to replace characters
System.out.print("After replacing all o with r: ");
System.out.println(s.replace('o', 'r'));
}
}
Output

Explanation
In the above program, string variable s is assigned to "Hello everyone". Using the predefined method replace(), we have replaced the character 'e' with 'x' and 'o' with 'r'.
2. Replace(CharSequence target, CharSequence replacement) method
- Program for replace method to replace character sequence
public class Main{
public static void main(String args[]){
//string declaration
String s1="It is rainy";
System.out.print("Original string is: ");
System.out.println(s1);
String replaceString=s1.replace("is","was");//replacing all occurrences of "is" to "was"
System.out.print("After replacement: ");
System.out.println(replaceString);
}}
Output

Explanation
In the above program, string variable s1 is assigned to “It is rainy". Using the predefined method replace(), we have replaced the word ‘is’ with ‘was'.
- Program for replacing method to replace character sequence
public class Main
{
// main method
public static void main(String argvs[])
{
String s = "Java programming language is very important ";
int size = s.length();
System.out.println(s);
String t = null;
// replacing null with Python. Hence, the NullPointerException is raised.
s = s.replace(t, "Python");
System.out.println(s);
}
}
Output

Explanation
In the above program, string variable s is assigned to "Java programming language is very important ". Using the predefined method, replace(), we have replaced the variable t with the name "Python".
Replace First()
This function replaces the first word if the passed String is matched.
Syntax:
public String replace First(String original, String replace)
Program for replacing the first method
public class Main {
public static void main(String args[])
{
// String Initialisation
String Str = new String("Hi Everyone");
// original string
System.out.print("Original String : ");
System.out.println(Str);
System.out.print("Replacing first occurrence of regex with replace_str : ");
System.out.println(
Str.replaceFirst("Hi", "With"));
}
}
Output

Explanation
In the above program, string variable s is assigned to "Hi Everyone ". Using the predefined method replaceFirst(), we have replaced the word “Hi” with the word “with".
Replace all
The method uses the provided replace str to replace each substring of the String that matches the provided regular expression.
Syntax:
public String replaceAll(String regex, String replace_str)
1. Program for string replace
public class Main{
public static void main(String args[]){
System.out.print("Original string: ");
String s1="Hello everyone";
System.out.println(s1);
System.out.println("Reverse string: ");
String replaceString=s1.replaceAll("e","a"); //replaces all occurrences of "a" to "e"
System.out.println(replaceString);
}
}
Output

Explanation
In the above program, string variable s is assigned to "Hi Everyone ". Using the predefined method replaceAll(), we have replaced the character 'e' with the character ' a'.
2. Program for String replace
public class Main{
public static void main(String s[])
{
// Initialising String
String Str = new String("Hello everyone");
// original string
System.out.print("Original String : ");
System.out.println(Str);
System.out.print("Replacing regex with replace_str : ");
System.out.println(Str.replaceAll("Hello", "Hi"));
}
}
Output

Explanation
In the above program, string variable s is assigned to "Hello Everyone ". Using the predefined method replaceAll(), we have replaced the word “Hello” with “Hi”.
3. Program for String replace
public class Main
{
public static void main(String args[])
{
String s = "Hi everyone";
System.out.print("Original string is :");
System.out.println(s);
String r = "";
System.out.println("replaced string is: ");
s = s.replaceAll(r," ");
System.out.println(s);
}
}
Output

Explanation
In the above program, string variable s is assigned to "Hi Everyone ". Using the predefined method replaceAll(), we have replaced the word "Hi” with “ ”.