Home >Java >javaTutorial >How to Reliably Split Java Strings by Newline Characters?
Splitting Java Strings by New Line
When attempting to split a String within a JTextArea using a regular expression to separate it by new lines using n, you may encounter difficulties. This issue arises because the system might utilize distinct newline conventions.
To address this problem effectively, it is crucial to employ a regular expression that accounts for all possible newline conventions. For instance, you could utilize the following regex:
String lines[] = string.split("\r?\n");
This regex will successfully split the string at both Windows (CRLF) and Unix (LF) newline characters.
The above is the detailed content of How to Reliably Split Java Strings by Newline Characters?. For more information, please follow other related articles on the PHP Chinese website!