Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split text file into Strings on empty line

Tags:

java

regex

split

I want to read a local txt file and read the text in this file. After that i want to split this whole text into Strings like in the example below .

Example : Lets say file contains-

 abcdef                                 
 ghijkl

 aededd               
 ededed

 ededfe
 efefeef
 efefeff

 ......
 ......

I want to split this text in to Strings

s1 = abcdef+"\n"+ghijkl;

s2 = aededd+"\n"+ededed; 

s3 = ededfe+"\n"+efefeef+"\n"+efefeff;

........................

I mean I want to split text on empty line.

I do know how to read a file. I want help in splitting the text in to strings

like image 238
Sunny Avatar asked Apr 08 '12 19:04

Sunny


People also ask

How do I split text in an empty line?

To split text by empty line, split the string on two newline characters, e.g. my_str. split('\n\n') for POSIX encoded files and my_str. split('\r\n\r\n') for Windows encoded files.

How do you split a string in a text file?

You can use String. split() method (in your case it's str. split("\\s+"); ).

How do you split a text file into lines in Python?

The fastest way to split text in Python is with the split() method. This is a built-in method that is useful for separating a string into its individual parts. The split() method will return a list of the elements in a string.


2 Answers

you can split a string to an array by

String.split();

if you want it by new lines it will be

String.split("\\n\\n");

UPDATE*

If I understand what you are saying then john.

then your code will essentially be

BufferedReader in
   = new BufferedReader(new FileReader("foo.txt"));

List<String> allStrings = new ArrayList<String>();
String str ="";
while(true)
{
    String tmp = in.readLine();
    if(tmp.isEmpty())
    {
      if(!str.isEmpty())
      {
          allStrings.add(str);
      }
      str= "";
    }
    else if(tmp==null)
    {
        break;
    }
    else
    {
       if(str.isEmpty())
       {
           str = tmp;
       }
       else
       { 
           str += "\\n" + tmp;
       }
    }
}

Might be what you are trying to parse.

Where allStrings is a list of all of your strings.

like image 65
Kevin Avatar answered Sep 20 '22 15:09

Kevin


I would suggest more general regexp:

text.split("(?m)^\\s*$");

In this case it would work correctly on any end-of-line convention, and also would treat the same empty and blank-space-only lines.

like image 32
grayswander Avatar answered Sep 19 '22 15:09

grayswander