Split Java String by New Line

前端 未结 20 1084
予麋鹿
予麋鹿 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:35

    If, for some reason, you don't want to use String.split (for example, because of regular expressions) and you want to use functional programming on Java 8 or newer:

    List lines = new BufferedReader(new StringReader(string))
            .lines()
            .collect(Collectors.toList());
    

提交回复
热议问题