Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read nth line from string

i am trying to read 7th line of a string so that i can filter the required text but not getting more.(assuming i have n number of line).

class Lastnthchar {

public static void main(String[] args) {
    // TODO Auto-generated method stub


    String alldata ="   FORM"+"\n"+
                    "   to get all data"+"\n"+
                    "   PART A is mandatory"+"\n"+
                    "   enclose all Certificate"+"\n"+
                    "   Certificate No. SFDSFDFS Last updated on 12-Jun-2009"+"\n"+
                    "   Name and address"+"\n"+
                    "   Lisa Lawerence"+"\n"+
                    "   10/3 TOP FLOOR, Street no 22 ,NewYork"+"\n"+
                    "   residence"+"\n"+
                    "   zip-21232"+"\n"+
                    "   C 78,New York"+"\n"+
                    "   US"+"\n"+
                    "   US"+"\n"+
                    "   "+"\n"+
                    "   worldwide";

    String  namerequired = new String ();

    //BufferedReader br = new BufferedReader(alldata);
    int lineno = 0;
    for(lineno = 0; lineno <alldata.length(); lineno ++)
    {
    //what should i do?
    }
    }

    }

so if any solution please help.

like image 341
dev_android Avatar asked Jul 16 '26 07:07

dev_android


1 Answers

alldata.length() will return the length of the string (i.e. number of characters), not the number of lines.

To get the nth line you'll need to split the string at the line breaks, e.g. alldata.split("\n")[6] to get the 7th line (provided there are at least 7 lines). This also assumes you have line breaks (\n) in your string and not just carriage returns (\r). If you want to split at both individually or in combination, you can change the parameter of split() to "\r\n|\n|\r". If you want to skip empty lines, you can split at any sequence of at least one line break or carriage return, e.g. "[\r\n]+".

Example:

System.out.println("--- Input:");
String input = "A\nB\rC\n\nD\r\nE";
System.out.println(input);  

System.out.println("--- 4th element, split by \\n:");
System.out.println(input.split("\n")[3]); //3rd element will be "D\r"
System.out.println("--- 4th element, split by \\r\\n|\\n|\\r:");
System.out.println(input.split("\r\n|\n|\r")[3]); //3rd element will be an empty string 
System.out.println("--- 4th element, split by [\\r\\n]+:");
System.out.println(input.split("[\r\n]+")[3]); //3rd element will be "D"
System.out.println("--- END");

Output:

--- Input:
A
B
C

D
E
--- 4th element, split by \n:
D

--- 4th element, split by \r\n|\n|\r: 

--- 4th element, split by [\r\n]+:
D
--- END

Alternatively, if you're reading the text from some stream (e.g. from a file) you can use BufferedReader#readLine() and count the lines. Additionally you can initialize the BufferedReader with a FileReader, StringReader etc., depending on where you read the input from.

If you're reading from the console, the Console class also has a readLine() method.

like image 85
Thomas Avatar answered Jul 17 '26 21:07

Thomas