Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To get last word of string in typeScript, use split(" ") method with strArr.length - 1 index value it will return last word from string. You have to only pass " " in split() method.

Let’s see short example to use split(" ") with with strArr.length - 1 index to get last word from string.

let strArr: Array<string> = string.split(" ");
strArr[strArr.length - 1];

Today, I’m going to show you How do I get last word of string in typeScript, as above mentioned, I’m going to use the above-mentioned split() method.

Let’s start today’s tutorial how do you get last word of string in typeScript?

TypeScript get last word of string example

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

let string: string = "Welcome to infinitbility";

let strArr: Array<string> = string.split(" ");

if(strArr.length > 0){
  console.log(strArr[strArr.length - 1])
}

// Output
// 'infinitbility'

***TIP: Before acceing any array value using index check length greater than 0 or not ***

Output

typeScript get last word of string example
typeScript get last word of string example

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