How to use while loop and charAt In Typescript
In this approach we initialize the string str. We use a while loop to iterate over each character. The loop runs as long as i is less than the length of the string. Inside the loop, we use str.charAt(i) to get the character at the current index i. We increment i after each iteration to move to the next character.
Example: Below is the implementation of the above-discussed approach.
let str: string = "GFG";
let i: number = 0;
while (i < str.length) {
console.log(str.charAt(i));
i++;
}
Output:
G
F
G
Iterate Over Characters of a String in TypeScript
Iterating over characters of a string involves going through them one by one using loops or specific methods. This is useful for tasks like changing or analyzing the content of a string efficiently.
Example:
Input: string = "Hello Geeks"; Output: H e l l o G e e k s
Below listed methods can be used to iterate over characters of a string in TypeScript.
Table of Content
- Example:
- Using for Loop
- Using for…of Loop
- Using split() Method
- Using forEach Method with split()
- Using the spread operator
- Using Array.from() Method
- Using while loop and charAt
Contact Us