Languages
[Edit]
EN

JavaScript - split string

12 points
Created by:
Ela-Davey
663

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

1. Splitting string by text

Edit

Each string can be split in JavaScript with other strings.

1.1. Split string by space example

Edit

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

1.2. Split string by some text example

Edit

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

1.3. Split string to array of characters example

Edit

In this approach empty string is used to split the main string into an array of characters.

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.

2. Split string with regex (regular expression)

Edit

Each string can be split in JavaScript with regular expression.

2.1. Split string by white character symbol example

Edit

In this example, the white character symbol is used to split text into single words.

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

Edit

In this example, a white character symbol is used to split text into single words limiting output array size to two elements.

3. Split string with Symbol.split (with splitter) example

Edit

An example of how to use a splitter is described in this article.

References

Edit
  1. String.prototype.split - MDN Docs 

Alternative titles

  1. JavaScript - different ways how to split string
  2. JavaScript - different approaches how to split string
  3. JavaScript - string splitting
  4. JavaScript - convert string to array
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.

JavaScript - string conversion

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