I am currently retrieving some information from a text file (.txt) that contains some paragraphs. When I retrieve the String from the text file I want to split it so that I each paragraph is in a String object.
Here is the text I get from the text file:
http://www.carlowweather.com/plaintext.txt
I have tried to split the String using line breaks and return carriage feeds but neither appear to work, see my code below:
int pCount=0;
public void parseData(String data){
String regex = "(\\n)";
String split[] = data.split(regex);
for(int i = 0; i<split.length; i++){
Log.e("e", pCount + " " + split[i]);
pCount ++;
}
}
I have also tried “\r” and various combinations I have found via searching the net but none seem to work on Android with this text file, I’m guessing the file doesn’t contain line breaks or carriage returns? But just blank lines?
What is the best way to split the paragraphs into String objects?
The below code will tell you where a new paragraph break exists. It will be up to you to deal with it after that. It simply looks for lines with ” ” only.
This is a characteristic of the file you have referred to. I have included the process used to read the file in the code sample below, as you did not specify that in your original question. One thought I had was that you were reading the file line by line and then trying to do the regEx on each line. I would assume that the previous suggestions would work if you read all of the text file into the one String.
Also, you could break the code up below into another function.