SUBSTRING REMOVAL USING STRINGBUFFER CLASS
import java.io.*;
class Removal
{
String str,sub;
int i=0,flag=0;
void getString() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("Enter the main String:");
str=br.readLine();
System.out.println("Enter the Substring:");
sub=br.readLine();
}
void removeString()
{
StringBuffer strbuf=new StringBuffer(str);
int l1,l2,l=0;
l1=strbuf.length();
l2=sub.length();
if((l2>l1)||(l2<0))
System.out.print("Invalid String");
else
{
do
{
String s=strbuf.substring(i,i+l2);
if(s.equals(sub))
{
strbuf.delete(i,i+l2);
flag=1;
break;
}
i++;
l=strbuf.length();
}while((l-i)>=l2);
if(flag==0)
System.out.println("No Such Substring Exist");
else
System.out.println("New String is:"+strbuf);
}
}
}
class Ex2
{
public static void main(String args[]) throws IOException
{
Removal r=new Removal();
r.getString();
r.removeString();
}
}
OUTPUT
D:\AMJPracticals\JAVA>javac Ex2.java
D:\AMJPracticals\JAVA>java Ex2
Enter the main String:
AM JAIN COLLEGE
Enter the Substring:
AM
New String is: JAIN COLLEGE