从字符串中读取第n行

时间:2014-10-14 09:39:32

标签: java string filter

我正在尝试读取字符串的第7行,以便我可以过滤所需的文本但不会获得更多。(假设我有n行)。

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?
    }
    }

    }

所以,如果有任何解决方案,请帮助。

3 个答案:

答案 0 :(得分:8)

alldata.length()将返回字符串的长度(即字符数),而不是行数。

要获得n th 行,您需要在换行符处拆分字符串,例如 alldata.split("\n")[6]获得7 th 行(前提是至少有7行)。 这也假设您的字符串中有换行符(\n),而不仅仅是回车符(\r)。如果您想单独或组合拆分,可以将split()的参数更改为"\r\n|\n|\r"。如果要跳过空行,可以按至少一个换行符或回车符的任意顺序进行拆分,例如"[\r\n]+"

示例:

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");

输出:

--- 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

或者,如果您正在从某个流(例如,从文件中)读取文本,则可以使用BufferedReader#readLine()并计算行数。此外,您可以使用BufferedReaderFileReader等初始化StringReader,具体取决于您从中读取输入的位置。

如果您正在从控制台阅读,Console课程也采用readLine()方法。

答案 1 :(得分:4)

如果您使用BufferedReader,您可以执行以下操作:

class Lastnthchar {

    public static void main(String[] args) throws IOException {

        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";

        BufferedReader br = new BufferedReader(new StringReader(alldata));

        String namerequired;
        String line;
        int counter = 0;
        while ((line = br.readLine()) != null) {
            if (counter == 6) {
                namerequired = line;
            }
            counter++;
        }
    }
}

答案 2 :(得分:1)

解决问题的一种方法是检查&#34; \ n&#34;的索引。指定的次数,直到找到所需的行。我把它写在了脑海中,所以如果语法不是100%准确,我很抱歉,但逻辑在这里:

public String readSpecifiedLine(String str, int lineNumber){
   int lineStartIndex = 0; 
   //start by finding start of specified line
   for(int i=0;i<lineNumber;i++){
      lineStartIndex = str.IndexOf("\n",lineStartIndex); //find new line symbol from
      //specified index
      lineStartIndex++; //increase the index by 1 so the to skip newLine Symbol on
      //next search or substring method
      //Note, you might need to increase by 2 if "\n" counts as 2 characters in a string
}
int nextLine = str.IndexOf("\n",lineStartIndex); //end of line 7
retrun str.substring(lineStartIndex,nextline);
}

您可能需要使用索引