Split Java String by New Line

前端 未结 20 1129
予麋鹿
予麋鹿 2020-11-22 00:56

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|

20条回答
  •  攒了一身酷
    2020-11-22 01:52

    • try this hope it was helpful for you

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

提交回复
热议问题