Just as in JavaScript, which is a subset of TypeScript, the split()
method exists. This method splits a string into an array of its substrings. All it needs is a separator that tells how to split the string into substrings.
string.split(separator,[limit])
separator
: This tells TypeScript where to start splitting the string.
limit
: This is the limit of elements of the array returned. It is the number of elements we want to see returned.
The value returned is an array.
export {}// create some stringslet greeting:string = "Welcome to Edpresso"let fruits:string = "Apple, Orange, Mango, Banana"let numbers:string = "one-two-three-four-five"let url:string = "https://www.educative.io/index.html"// split the strings to arrayslet split1 = greeting.split(" ");let split2 = fruits.split(",")let split3 = numbers.split("-", 2) // return just 2 elementslet split4 = url.split(".")// print the arraysconsole.log(split1)console.log(split2)console.log(split3)console.log(split4)