find the last character in a string in typescript

To find the last character of a string in TypeScript, you can use the index notation with the length of the string minus one. Here's an example:

index.ts
const myString: string = 'Hello, World!';
const lastCharacter: string = myString[myString.length - 1];
console.log(lastCharacter); // Output: "!"
146 chars
4 lines

In this code snippet, we declare a myString variable and assign it the value of "Hello, World!". Then, we declare a lastCharacter variable and use the index notation with myString.length - 1 to access the last character of the string, which is "!". Finally, we log the lastCharacter variable to the console.

Note that strings in TypeScript are zero-indexed, meaning that the first character of the string is at index 0 and the last character is at index myString.length - 1.

related categories

gistlibby LogSnag