find the first character in a string in typescript

You can use indexing, charAt, or substring to find the first character of a string in TypeScript.

index.ts
const str: string = "hello";
const firstCharIndex: number = 0;

// Option 1: Indexing
const firstChar: string = str[firstCharIndex];

// Option 2: charAt
const firstChar2: string = str.charAt(firstCharIndex);

// Option 3: substring
const firstChar3: string = str.substring(firstCharIndex, firstCharIndex + 1);
311 chars
12 lines

All three options should return the first character of the string, which is "h". Note that indexing is the most efficient way to get a single character from a string.

gistlibby LogSnag