EN
TypeScript - get nth string character
0
points
In this article, we would like to show you how to get n-th string character in TypeScript.
Quick solution:
const text: string = 'Dirask';
console.log(text.charAt(0)); // D
// ^
// |
// character on 1st position (index=0)
or:
const text: string = 'Dirask';
console.log(text[0]); // D
Practical examples
1. Using String charAt() method
In this example, we use built-in String charAt() method to get n-th string character.
const text: string = 'Dirask';
const index: number = 3;
const character: string = text.charAt(index);
console.log(character); // a
2. Using square brackets ([]) notation
String characters can be accessed with [] like in array case.
const text: string = 'Dirask';
const index: number = 3;
const character: string = text[index];
console.log(character); // a