Navigate back to the homepage

JavaScript - String split() function

Vishnu Damwala
April 5th, 2020 · 2 min read

The split() function separates an original string into an array of substrings.

The split() method divides a String using a specified separator into an ordered set of substrings, returning a resultant array.

The division is performed on provided search pattern i.e separator; provided as the first parameter in the method’s call.


Syntax:

1str.split([separator[, limit]]);

Parameters:

separator (optional)

The separator is a pattern describing where each split should occur. It can be a simple string or a regular expression.

  • A separator parameter having a single character is the simplest one. For example, a string containing comma-separated values (CSV) could be parsed by passing a comma(,) character as the separator, such as csvString.split(",").
  • The separator can also contain multiple characters, so to split the string that the entire character sequence must match.
  • If a separator is an empty string (""), then the resultant array will have each of the string’s UTF-16 “characters” as a single element.
limit (optional)
  • A non-negative integer limiting the number of elements. Specifying the limit returns the number of elements after splitting the string. The leftover text is not included in the array at all.
  • The array may contain fewer entries than the limit; if the end of the string is reached before the limit is reached.
  • If the limit is reached before reaching the end of the string then the resultant array will have fewer elements.
  • If the end of the string is reached before reaching then limit then the resultant array will have all elements after splitting the string
  • If the limit is 0, no splitting is performed.
  • If a negative integer is given as limit then all elements after splitting are returned i.e negative will be ignored

Example 1

1let quote = "An ounce of practice is worth a thousand words."
2quote.split(" ")
3
4// Output
5// ["An", "ounce", "of", "practice", "is", "worth", "a", "thousand", "words."]

Example 2

1// Namaste 🙏 means "bowing to you"
2let greet = "Namaste"
3greet.split("")
4
5// Output
6// ["N", "a", "m", "a", "s", "t", "e"]

In the above example string split function will split the original string into individual characters.

If you do not specify any separator character like split(""), then split function will return an array of all individual characters.

Example 3

1let quote = "An ounce of practice is worth a thousand words."
2quote.split(" ", 2)
3
4// Output
5// ["An", "ounce"]

If you specify limit character, then split function will return a specified number of elements in the resultant array.

In the above example, only "An" and "ounce" are returned by the split function, as 2 is passed as the limit value.

Example 4

1"1+2+3+4+5+6+7+8+9".split("+")
2
3// Output
4// ["1", "2", "3", "4", "5", "6", "7", "8", "9"]

In the above example string split function can also be directly used with string. In case you might not want to declare a variable.

Example 5

1"1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9".split(" + ", 4)
2
3// Output
4// ["1", "2", "3", "4"]

Also, string separator does not have to be a single character, it can be any multiple or combination of characters.

In the above example separator " + " (whitespace, plus, whitespace) and limit 4 will split string with a separator having multiple characters and returns only 4 elements at the last.

Common uses of split() function

  • Creating or extracting an array of words from a sentence:
1let sentence = "A baby is born with a need to be loved and never outgrows it."
2sentence.split(" ")
3
4// Output
5/*
6 ["A", "baby", "is", "born", "with", "a", "need", "to",
7 "be", "loved", "and", "never", "outgrows", "it."]
8*/
  • Reversing the letters in a string or word:
1let word = "rehtoM"
2word.split("").reverse().join("")
1"Mother"

Hope you learn something new. If this article was helpful, share it.

Happy coding

More articles from MeshWorld

Chrome 80 brings support for let and class re-declarations in the Console

Re-declarations of let and class keyword are now allowed in console in Chrome 80 makes debugging more developer friendly

April 3rd, 2020 · 1 min read

SQL - CREATE TABLE Statement

A tutorial for creating database table with SQL

April 2nd, 2020 · 1 min read
© 2017–2020 MeshWorld
Link to $https://twitter.com/meshworldindiaLink to $https://facebook.com/meshworldindiaLink to $https://instagram.com/meshworld.india/