62 of 264 menu

split method

The split method splits string into an array according to the separator (delimiter) specified in the first parameter. If it is not set, the entire string will be returned. If it is given as empty quotes, then each character of the string will go into a separate element of the array. As the second optional parameter, you can specify the maximum number of elements in the resulting array.

Syntax

string.split([separator], [maximum number of elements]);

Example

Let some string with hyphens be given. Let's split this string into an array by the separator '-':

let str = 'ab-cd-ef'; let arr = str.split('-'); console.log(arr);

The code execution result:

['ab', 'cd', 'ef']

Example

Let's again split a string by the delimiter '-', but specify the maximum number of elements in the resulting array as the second parameter, for example, 2. In this case, only 2 elements will be written to the array:

let str = 'ab-cd-ef'; let arr = str.split('-', 2); console.log(arr);

The code execution result:

['ab', 'cd']

Example

Let's write each character of a string into a separate element of the array:

let str = 'abcde'; let arr = str.split(''); console.log(arr);

The code execution result:

['a', 'b', 'c', 'd', 'e']

Example

Let's write the first 3 characters of a string into a separate elements of an array:

let str = 'abcde'; let arr = str.split('', 3); console.log(arr);

The code execution result:

['a', 'b', 'c']

Example

Let's use split to split a string of digits. Note that the result will be an array of strings, not an array of numbers:

let str = '12345'; let arr = str.split(''); console.log(arr);

The code execution result:

['1', '2', '3', '4', '5']

Example . Usage

Let's reverse the characters of a string. To do this, split the string into an array using split with the separator '' (this will place each character of the string in a separate element of the array), reverse this array using reverse and then join the reversed array back with join:

let str = '123456789'; let arr1 = str.split(''); let arr2 = arr1.reverse(); let res = arr2.join(''); console.log(res);

The code execution result:

'987654321'

Example . Usage

Let's simplify the solution of the previous problem - let's merge all the commands into single chain:

let str = '123456789'; let res = str.split('').reverse().join(''); console.log(res);

The code execution result:

'987654321'

Example . Usage

Given a string with digits. Let's find the sum of the digits from this string. To do this, let's split the string into an array, and then iterate through this array and find its sum. We have a catch: split returns strings, so when summing, we convert these string digits into real digits using Number:

let str = '12345'; let arr = str.split(''); let sum = 0; for (let i = 0; i < arr.length; i++) { sum += Number(arr[i]); } console.log(sum);

The code execution result:

15

Example . Usage

Given a number. Let's write each digit of this number into a separate element of the array. There is a catch here - split only applies to strings, and we have a number. We convert number to string first with String and then apply split:

let num = 12345; let str = String(num); let arr = str.split(''); console.log(arr);

The code execution result:

['1', '2', '3', '4', '5']

See also

  • the join method
    that merges array elements into a string
  • the split method
    that splits a string by regex
  • the reverse method
    that reverses the order of elements in an array
  • the length property
    that allows you to find the length of a string
byenru