Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

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 combinations 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");

}

1 Answer

0 votes
by (46k points)

This should satisfy you:

String lines[] = string.split("\\r?\\n");

There are simply just two newlines (UNIX and Windows) that you require to despair on.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...