Search code examples
javasubstring

Extract substring from text


I'm writing a parser, where it reads all lines in a particular file and processes it!! I got stuck with this substring Abc [123] where ABC should be stored at as one string, and 123 as other string.so i came up with this solution

lines.get(i).substring(0,lines.get(i).lastIndexOf("["))

this get me the string abc

lines.get(i).substring(lines.get(i).lastIndexOf("["));

and aboveone gets me the string 123] but i dont want ] at last any updation can i make for my approach?


Solution

  • Change

    lines.get(i).substring(lines.get(i).lastIndexOf("["));
    

    to

    lines.get(i).substring(lines.get(i).lastIndexOf("[") + 1,lines.get(i).lastIndexOf("]"));