Split text file into Strings on empty line
java, regex, split
Solution
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.
Problem
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