Java program for string replacement…


This program will replace a string with another string. It means it will search the string which is to be replaced and it will replace it with the string which is asked to be put at that position.

class StringReplace
{
public static void main(String args [])
{
String org=”This is a test. This is. too.”;
String search=”is”;
String sub=”was”;
String result=””;
int i;

do
{
System.out.println(org);
i=org.indexOf(search);
if(i!=-1)
{
result=org.substring(0,i);
result=result+sub;
result=result+org.substring(i+search.length());
org=result;
}

}while(i!=-1);
}
}

Add Comment