EN
TypeScript - remove first n characters from string
0 points
In TypeScript it is possible to remove the first n characters from the string in the following way.
xxxxxxxxxx
1
const removeFirstChars = (text: string, n: number) => {
2
return text.slice(n);
3
};
4
5
console.log(removeFirstChars('12345', 0)); // 12345
6
console.log(removeFirstChars('12345', 1)); // 2345
7
console.log(removeFirstChars('12345', 2)); // 345
8
console.log(removeFirstChars('12345', 3)); // 34
9
console.log(removeFirstChars('12345', 4)); // 5
10
11
// below results are empty, that's why we compare them to ''
12
console.log(removeFirstChars('12345', 5) === ''); // true
Note:
We can achieve the same results using:
- String
substring()
method- String
replace()
method