Languages
[Edit]
EN

Java - split string by new line character

9 points
Created by:
Explosssive
559

In this article, we're going to have a look at the problem how to split string to separated lines in Java.

Quick solution:

  • It works on Linux and Windows:
  • It works on all operating systems:

or:

 

Look at below problem description and examples to see how it works in practice.

1. Problem overview

Edit

Different operating systems have different newline symbols.

There are a few most commonly used new line separations:

\nMultics, Unix and Unix-like systems (Linux, macOS, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS, and others.
\r\nAtari TOS, Microsoft Windows, DOS (MS-DOS, PC DOS, etc.), DEC TOPS-10, RT-11, CP/M, MP/M, OS/2, Symbian OS, Palm OS, Amstrad CPC, and most other early non-Unix and non-IBM operating systems.
\rCommodore 8-bit machines (C64, C128), Acorn BBC, ZX Spectrum, TRS-80, Apple II series, Oberon, the classic Mac OS, MIT Lisp Machine and OS-9
\n\rAcorn BBC and RISC OS spooled text output.

Source: https://en.wikipedia.org/wiki/Newline

2. Universal new lines splitting example

Edit

This approach works with all operating systems. The presented example shows splitting to separated lines on mixed text.

Output:

Note:

above expression symbols order is very important to try to:

  • split text by two characters as newline symbol at first (\r\n or \n\r),
  • and later with single newline symbol (\n or \r).

let's suppose we have HTTP protocol response:

for \n or \r at begining of the expression we could get different number of newlines after splitting.

after splitting we should get:

Second important thing is newline symbol unification per operationg system that makes posible to use below expression. 

3. Microsoft Windows new lines splitting example

Edit

Output:

Systems: Atari TOS, Microsoft Windows, DOS (MS-DOS, PC DOS, etc.), DEC TOPS-10, RT-11, CP/M, MP/M, OS/2, Symbian OS, Palm OS, Amstrad CPC, and most other early non-Unix and non-IBM operating systems.

4. Unix/Linux new lines splitting example

Edit

Output:

Systems: Multics, Unix and Unix-like systems (Linux, macOS, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS, and others.

5.  The classic Mac OS/OS-9 new lines splitting example

Edit

Output:

Systems: Commodore 8-bit machines (C64, C128), Acorn BBC, ZX Spectrum, TRS-80, Apple II series, Oberon, the classic Mac OS, MIT Lisp Machine and OS-9

 

6. The optimal way to split text by newline

Edit

Some split operations are executed many times in a source code. That makes sense to do not compile patterns inside String split() function each time when we call it - check split() function body. The improvement for the code can be to use Pattern class and create an object for it only once.

Example:

Output:

 

Alternative titles

  1. java split lines
  2. java split on newline
  3. java split by newline
  4. java split line break
  5. split by new line in java
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join