Home >Java >javaTutorial >How to Properly Split a Java String by Newline Characters?
How to Split a Java String by New Line
In order to split a string by new line in Java, you can use a regular expression. The most common way to do this is to use the n special character, which represents a line break. However, this may not always work, as there are different types of line breaks in different operating systems.
To accommodate for this, you can use the regular expression r?n to split the string by either a carriage return (r) or a line feed (n). This will work on both Windows and UNIX systems.
Here is an example of how to do this:
This will output the following:
The above is the detailed content of How to Properly Split a Java String by Newline Characters?. For more information, please follow other related articles on the PHP Chinese website!