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|
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.
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: