Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - split at a line break in String? (Paragraphs)

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?

like image 244
Donal Rafferty Avatar asked Feb 15 '26 16:02

Donal Rafferty


1 Answers

I think the easiest way to do this is with a Scanner.

Scanner sc = new Scanner(new File("donal.txt"), "UTF-8");
sc.useDelimiter("\n[ \t]*\n");

List<String> result = new ArrayList<String>();
int lineCount = 0;
while (sc.hasNext())
{
  String line = sc.next();
  System.out.printf("%n%d:%n%s%n", ++lineCount, line);
  result.add(line);
}
System.out.printf("%n%d paragraphs found.%n", lineCount);

The first and last paragraphs will actually be the header and footer; I don't know what you want to do about those.

For the sake of readability, I'm assuming the line separator is always the Unix-style \n, but to be safe you should allow for the Windows-style \r\n and older Mac-style \r as well. That would make the regex:

"(?:\r\n|[\r\n])[ \t]*(?:\r\n|[\r\n])
like image 117
Alan Moore Avatar answered Feb 18 '26 05:02

Alan Moore