· Write a Java program to extract a portion of a character string and print the extracted string.
Assume that in characters extracted, starting with the nth character,
Answers
Answer:
import java.io.*;
class Extract
{
publicstaticvoid main(String args[])
{
String s,str,substr;
int extract,start,len,check;
try{
BufferedReader obj = new BufferedReader(new InputStreamReader
(System.in));
System.out.print("Enter String : ");
System.out.flush();
str=obj.readLine();
len=str.length();
System.out.print("Enter Starting position to extract characters : ");
System.out.flush();
s=obj.readLine();
start=Integer.parseInt(s);
start=start-1;
if(start<0 || start>len)
{
System.out.println("INVALID POSITION");
System.exit(1);
}
System.out.print("Enter how many characters you want to extract
: ");
System.out.flush();
s=obj.readLine();
extract=Integer.parseInt(s);
check=extract+start;
if(check<0 || check>len )
{
System.out.println("TRYING TO EXTRACT INVALID POSITION");
System.exit(1);
}
substr=str.substring(start,check);
System.out.println("\nEXTRACTED STRING IS "+substr);
}
catch(Exception e) {}
}
}