This program accepts the string, old character and new character from the keyboard and replaces the old character with new character in the string.
import java.io.*;
/**
*
* @author JavaHotSpot
*/
public class stringReplace
{
public static void main(String args[])
{
String s1,s2,s3,s4;
DataInputStream d;
try
{
System.out.println("Enter the String to be manipulated");
d=new DataInputStream(System.in);
s1=(String)d.readLine();
System.out.println("Enter the Old Character");
s2=(String)d.readLine();
System.out.println("Enter the New Character");
s3=(String)d.readLine();
s4=s1.replace(s2, s3);
System.out.println("The String after manipulation "+s4);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Output:
Enter the String to be manipulated
Hello word
Enter the Old Character
word
Enter the New Character
Java
The String after manipulation Hello Java
Related Articles:
Java Programming Example of how to extract substring from the given string
Java Programming Example of how to remove the extra spaces from the given string
Java Programming Example of how to convert string into bytes
Java Programming Example of how to convert String into characters and display the character?
Java Programming Example to check whether the strings starts with given prefix and ends with given suffix or not?
Java Programming Example of finding the length of the given string
Java Programming Example of concatenation of two strings
Java Programming Example of converting all characters in the string upper case and vise versa
Java Programming Example to check whether the two strings are equal or not?