EN
TypeScript - split string by hyphen sign (minus sign - ascii 45 code)
0 points
In this article, we would like to show you how to split string by hyphen sign in TypeScript.
String split()
function takes as parameter character by which the string will be split into an array of substrings. As the result of String split()
we get an array. To access an element we just get an element by its index.
xxxxxxxxxx
1
const text: string = 'split-this-text';
2
const split: string[] = text.split('-');
3
4
const element1: string = split[0];
5
const element2: string = split[1];
6
const element3: string = split[2];
7
8
console.log(element1); // split
9
console.log(element2); // this
10
console.log(element3); // text
Output:
xxxxxxxxxx
1
split
2
this
3
text