Search code examples
javastringpositionchars

Java - Extract relevant strings/chars from specific index in file


I want to extract relevant strings/chars from specific indexe in a txt file. I know how to read the text file line by line but i need help with extracting relevant strings from fixed positions of the line.

lets say i have a txt file that has this content: "1234567891234567892Hello testingblabla"

I want to extract strings/chars according to this structure:

position 0-18: 123456789123456789

position 19-20: 2

position 20-40: Hellotestingblabla

I want this to be automated so that the next line gets extracted the same way as the first line that and so on. The values at those indexes will be different but the position will always be the same no matter what.

What's the best way to tackle these kind of problems? Any classes that are more useful than others?


Solution

  • I don't know exactly what you mean. Like this?

    String str="1234567891234567892Hello testingblabla";
    String str1=str.substring(0,18);
    String str2=str.substring(18,19);
    String str3=str.substring(20,40);