Languages
[Edit]
EN

TypeScript - replace last n characters in string

0 points
Created by:
Rian-Whitehouse
469

In this article, we're going to have a look at how to replace the last n characters from string in TypeScript.

1. String slice() method example

Edit

This approach allows getting substring by using negative indexes. So by using 0 and -n indexes as the range we get <0, text.length - n> text range. Then, we add the replacement at the end of the result string to replace the missing two characters.

Output:

2. String substring() method example

Edit

This is an alternative approach to slice method based solution. We remove the last n characters to add the replacement at the end of the result string to replace the missing n characters.

Output:

See also

Edit
  1. TypeScript - replace last character in string

  2. TypeScript - replace last 2 characters in string

  3. TypeScript - replace last 3 characters in string

  4. TypeScript - replace last 4 characters in string

1
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