Split Java String by New Line

2018-12-31 05:51发布

I'm trying to split text in a JTextArea using a regex to split the String by \n However, this does not work and I also tried by \r\n|\r|n and many other combination of regexes. Code:

public void insertUpdate(DocumentEvent e) {
    String split[], docStr = null;
    Document textAreaDoc = (Document)e.getDocument();

    try {
        docStr = textAreaDoc.getText(textAreaDoc.getStartPosition().getOffset(), textAreaDoc.getEndPosition().getOffset());
    } catch (BadLocationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    split = docStr.split("\\n");
}

19条回答
美炸的是我
2楼-- · 2018-12-31 06:19
package in.javadomain;

public class JavaSplit {

    public static void main(String[] args) {
        String input = "chennai\nvellore\ncoimbatore\nbangalore\narcot";
        System.out.println("Before split:\n");
        System.out.println(input);

        String[] inputSplitNewLine = input.split("\\n");
        System.out.println("\n After split:\n");
        for(int i=0; i<inputSplitNewLine.length; i++){
            System.out.println(inputSplitNewLine[i]);
        }
    }

}
查看更多
不流泪的眼
3楼-- · 2018-12-31 06:22

For preserving empty lines from getting squashed use:

String lines[] = String.split("\\r?\\n", -1);
查看更多
路过你的时光
4楼-- · 2018-12-31 06:22

All answers given here actually do not respect Javas definition of new lines as given in e.g. BufferedReader#readline. Java is accepting \n, \r and \r\n as new line. Some of the answers match multiple empty lines or malformed files. E..g. <sometext>\n\r\n<someothertext> when using [\r\n]+would result in two lines.

String lines[] = string.split("(\r\n|\r|\n)", -1);

In contrast, the answer above has the following properties:

  • it complies with Javas definition of a new line such as e.g. the BufferedReader is using it
  • it does not match multiple new lines
  • it does not remove trailing empty lines
查看更多
妖精总统
5楼-- · 2018-12-31 06:26

split method is using regex (regular expressions). Since Java 8 regex supports \R which represents (from documentation of Pattern class):

Linebreak matcher
\R         Any Unicode linebreak sequence, is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]

So we can use it to match:

As you see \r\n is placed at start of regex which ensures that regex will try to match this pair first, and only if that match fails it will try to match single character line separators.


So if you want to split on line separator use split("\\R").

If you don't want to remove from resulting array trailing empty strings "" use split(regex, limit) with negative limit parameter like split("\\R", -1).

If you want to treat one or more continues empty lines as single delimiter use split("\\R+").

查看更多
明月照影归
6楼-- · 2018-12-31 06:26
String.split(System.getProperty("line.separator"));

This should be system independent

查看更多
爱死公子算了
7楼-- · 2018-12-31 06:26

String lines[] =String.split( System.lineSeparator())

查看更多
登录 后发表回答