Java Blog

 
 
 
import java.io.*;

class Extract 
{
    public static void 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) {}


    }
}

 
 
Didn't find what you were looking for? Find more on Program to extract a portion of a character string and print the extracted string