Languages
[Edit]
EN

TypeScript - split string by new line character

0 points
Created by:
a_horse
538

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

Note: by default in TypeScript \n newline character should be used, but modern applications exchange information with external endpoints that force programmers to deal with different cases - this article provides quick solutuioins for the problem.

Quick solution:

Note: the above solution was inspired with new line characters that are used on MacOS, Linux and Windows.

 

Alternative quick solutions 

Edit
  • It works on all operating systems:
  • It is an alternative approach that uses \R equivalent:

 

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. Problem solutions

Edit

2.1. Universal new lines splitting example

Edit

It is possible to propose some universal regular expression that lest to get a very good splitting result.

It is necessary to use \r\n|\n\r|\n|\r as a pattern.

Go to this article to see details and runnable example.

2.2. Alternative new lines splitting example using \R equivalent

Edit

Some modern regular expression implementations provide \R predefined character class,
e.g. Java, Notepad++, etc.

Most TypeScript engines don't support \R yet - so, it is required some trick.

Go to this article to see details and runnable example.

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

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

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

Alternative titles

  1. TypeScript - split line break
  2. TypeScript - split on newline
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