How to divide a string from a Java string to a line

Asked 1 years ago, Updated 1 years ago, 97 views

In the JTextArea, I want to separate the strings by '\n'. The sauce below is not working properly. I've already tried '\r\n | \r | n'.

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

regex java split newline

2022-09-21 21:15

1 Answers

String lines[] = String.split("\\r?\\n"); You can do it in this way.


2022-09-21 21:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.