Languages
[Edit]
EN

TypeScript - remove first n characters from string

0 points
Created by:
a_horse
538

In TypeScript it is possible to remove the first n characters from the string in the following way.

1. Using slice() method

const removeFirstChars = (text: string, n: number) => {
  return text.slice(n);
};

console.log(removeFirstChars('12345', 0)); // 12345
console.log(removeFirstChars('12345', 1)); // 2345
console.log(removeFirstChars('12345', 2)); // 345
console.log(removeFirstChars('12345', 3)); // 34
console.log(removeFirstChars('12345', 4)); // 5

// below results are empty, that's why we compare them to ''
console.log(removeFirstChars('12345', 5) === ''); // true

Note:
We can achieve the same results using:

  • String substring() method
  • String replace() method

Alternative titles

  1. TypeScript - remove first n characters from string - js util
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join