Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To get first character of string in typescript, use substring() method to extracts first character from string. You have to only pass starting and ending postion of string so for first character pass 0, 1 as parameter it will return first character of string.

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

text.substring(0, 1);

Today, I’m going to show you How do I get first 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 first character of string in typescript?

Typescript get first character of string example using substring()

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

let str: string = "Infinitbility";

let char: string = str.substring(0, 1);
console.log(char);

// Output
// 'I'

Typescript get first character of string example using index

Here, we will take string variable with some data and use index method to get first char of string.

let str: string = "aGuideHub";

let char: string = str[0];
console.log(char);

// Output
// 'a'

Typescript get first character of string example using charAt()

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

let str: string = "SortoutCode";

let char: string = str.charAt(0);;
console.log(char);

// Output
// 'S'

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