TypeScript – How to Split String | string.split() method

  • by
TypeScript-How-to-Split-String

Using string.split() function in TypeScript we can split a string into an array of its substrings.  The split functions requires a separator which specifies how to separate the string.

Syntax:string.split(separator, [limit])

Separator: This will specifies the character using which the string will be split.

limit: This specifies the number of splits.

Example:

export{}

let days:string = "Sunday Monday Tuesday Wednesday"
let dayObj = days.split(" ");

console.log(dayObj);

Output:

[ 'Sunday', 'Monday', 'Tuesday', 'Wednesday' ]

Since we haven’t specified the number of splits, this will split the entire string with a space.

Example – With limit:
export{}

let days:string = "Sunday Monday Tuesday Wednesday"
let dayObj = days.split(" ",2);

console.log(dayObj);

Output:

[ 'Sunday', 'Monday' ]

Since the number of splits is supplied as 2, this will only return the first 2 objects.

Getting the objects from the split string:

Let’s consider you want to print only weekends from our parent string. Then we can specify its index:

Example:

export{}

let days:string = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday"
let dayObj = days.split(",",7);

console.log(dayObj[0],dayObj[6]);

This will return the object at 1st and 7th index from the array.

Output:

Sunday Saturday

What if the separator isn’t present in the string:

In case the specified separator for the split isn’t present then this TypeScript function will return parent string as its 1st object.

Example:

export{}

let days:string = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday"
let dayObj = days.split("z",7);

console.log(dayObj);
console.log(dayObj[0]);

Output:

[ 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday' ]
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday

Empty String:

If the parent string is empty, split function will return an empty string in the array.

Example:

export{}

let days:string = ""
let dayObj = days.split("z",7);

console.log(dayObj);

Output:

[ '' ]

Split with no separator:

Let’s consider that you aren’t specifying any split separator in the function. Then this TypeScript function will simply give you each character from the parent string as an object of the array.

Example:

export{}

let days:string = "Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
let dayObj = days.split("");

console.log(dayObj);

Output:

[
'S', 'u', 'n', 'd', 'a', 'y', ' ', 'M',
'o', 'n', 'd', 'a', 'y', ' ', 'T', 'u',
'e', 's', 'd', 'a', 'y', ' ', 'W', 'e',
'd', 'n', 'e', 's', 'd', 'a', 'y', ' ',
'T', 'h', 'u', 'r', 's', 'd', 'a', 'y',
' ', 'F', 'r', 'i', 'd', 'a', 'y', ' ',
'S', 'a', 't', 'u', 'r', 'd', 'a', 'y'
]

Split using Regex – Regular Expressions:

We can also split the string using regular expressions in TypeScript or JavaScript. Below example shows how to split string using Regex.

Example:

export{}

let continent:string = "Australia is the 6th largest continent, There are total 195 countries in the world."
let split1 = continent.split(/(\d)/);

console.log(split1);

Output:

[
'Australia is the ',
'6',
'th largest continent, There are total ',
'1',
'',
'9',
'',
'5',
' countries in the world.'
]

\d matches the character class for digits between 0 and 9. Because of which the string is split where ever there is a digit in the parent string.

If you found this article helpful, checkout more tutorial on TypeScript on our blog. please consider liking us on our Facebook page for regular updates. Thank you 🙂

ParallelCodes on Facebook

Source: MDN WebDocs


1