Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To get last two character of string in typescript, use substring() method to extracts last two character from string. You have to only pass starting and ending postion of string so for last two character pass text.length - 2 as parameter it will return last two character of string.

Let’s see short example to use substring() to get last two character from string.

text.substring(text.length - 2);

Today, I’m going to show you How do I get last two character of string in typescript, as above mentioned, I’m going to use the above-mentioned substring() method.

Let’s start today’s tutorial how do you get last two character of string in typescript?

Typescript get last two character of string example using substring()

Here, we will take string variable with some data and use substring() method to get last two char of string.

let str: string = "Infinitbility";

let char: string = str.substring(str.length - 2);
console.log(char);

// Output
// 'ty'

Output

Typescript get last two character of string example
Typescript get last two character of string example

I hope it helps you, All the best đź‘Ť.