Android的 - 斯普利特在字符串换行? (段) [英] Android - split at a line break in String? (Paragraphs)

查看:162
本文介绍了Android的 - 斯普利特在字符串换行? (段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前检索从包含某些段落文本文件(.txt)的一些信息。当我检索的文本文件中的字符串我想拆它,这样我每个段落是一个String对象。

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.

下面是我从文本文件获取文本: http://www.carlowweather.com/plaintext.txt

Here is the text I get from the text file: http://www.carlowweather.com/plaintext.txt

我曾尝试使用换行符分割字符串,返回车厢饲料,但似乎都不上班,看我下面的code:

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 ++;
    }
}

我也曾尝试\ r和各种组合我通过搜索网已经找到,但没有一个似乎工作在Android上使用这个文本文件,我猜不包含换行符或回车的文件吗?但就空行?

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?

什么是段落分割成String对象的最佳方式是什么?

What is the best way to split the paragraphs into String objects?

推荐答案

以下code会告诉你,一个新的段落突破存在。这将是由你以后处理它。它只是看起来与,专用线。 这是已提到的文件的特性。我已经包括用于读取低于code样品中的文件,因为你没有指定,在你原来的问题的过程。我想过的是,你读一行文件行,然后尝试做每行的正则表达式。我会假设previous建议将工作,如果你阅读所有的文本文件到一个字符串。

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.

此外,您可以跌破code成另一种功能。

Also, you could break the code up below into another function.

        try {
        BufferedReader in = new BufferedReader(new FileReader("plaintext.txt"));
        String inputDataLine;
        while ((inputDataLine = in.readLine()) != null) {
            if (!(inputDataLine.contentEquals(" "))) {
                System.out.println("What you want to do with a paragraph line");
            } else {
                System.out.println("What you want to do with a paragraph seperator");
            }
        }
        in.close();
    } catch (IOException e) {
    }

这篇关于Android的 - 斯普利特在字符串换行? (段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆