Languages
[Edit]
EN

TypeScript - split string

0 points
Created by:
Richard-Bevan
413

This article focuses on different ways how to split string to array. TypeScript provides String.prototype.split method that can be used to do it.

1. Splitting string by text

Edit

Each string can be splitted in TypeScript with other string.

1.1. Split string by space example

Edit

In this example string with single space is used to spltit text to single words.

Output:

1.2. Split string by some text example

Edit

This section shows how to split string by some other string.

Output:

1.3. Split string to array of characters example

Edit

In this apprach empty string is used to split main string into array of characters.

Output:

 

Note: this approach is strongly not recommended because of unicode characters that can be misinterpreted.

1.4. Split string by space with result size limit example

Edit

This section shows how to split string by space limiting output array size to two elements.

Output:

2. Split string with regex (regular expression)

Edit

Each string can be splitted in TypeScript with regular expresion.

2.1. Split string by white character symbol example

Edit

In this example white character symbol is used to spltit text to single words.

Output:

2.2. Split string by white character symbol with result size limit example

Edit

In this example white character symbol is used to spltit text to single words limiting output array size to two elements.

Output:

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