How to Iterate Over Characters of a String in TypeScript ?
Last Updated :
25 Jun, 2024
In Typescript, iterating over the characters of the string can be done using various approaches. We can use loops, split() method approach to iterate over the characters.
Below are the possible approaches:
Using for Loop
In this approach, we are using a for loop to iterate over a string’s characters by indexing each character based on the length of the string and accessing characters at each index.
Syntax:
for (initialization; condition; increment/decrement) {
// code
}
Example: Below is the implementation of the above-discussed approach.
JavaScript
let inputStr: string = "GeeksforGeeks";
for (let i = 0; i < inputStr.length; i++) {
console.log(inputStr[i]);
}
Output:
G
e
e
k
s
f
o
r
G
e
e
k
s
Using forEach() method
In this approach, we are using the forEach() method on an array which is created from the string to iterate through characters separately.
Syntax:
array.forEach((element: ElementType, index: number, array: ArrayType) => {
// code
});
Example: Below is the implementation of the above-discussed approach.
JavaScript
let inputStr: string = "GeeksforGeeks";
Array.from(inputStr).forEach(char => {
console.log(char);
});
Output:
G
e
e
k
s
f
o
r
G
e
e
k
s
Using split() method
In this approach, we Split the string into an array of characters using the split() method, then iterate through the array to process each character separately.
Syntax:
let arrayFromSplit: string[] = stringToSplit.split(separator, limit);
Example: Below is the implementation of the above-discussed approach.
JavaScript
let inputStr: string = "GeeksforGeeks";
inputStr.split('').forEach(char => {
console.log(char);
});
Output:
G
e
e
k
s
f
o
r
G
e
e
k
s
Using for...of Loop
In this approach, we leverage the for...of loop in TypeScript to iterate directly over the characters of the string without the need for explicit indexing or converting the string into an array.
Syntax:
for (const char of inputStr) {
// code
}
Example: Below is the implementation of the above-discussed approach.
JavaScript
let inputStr: string = "GeeksforGeeks";
for (const char of inputStr) {
console.log(char);
}
Output:
G
e
e
k
s
f
o
r
G
e
e
k
s
Using map() Method
In this approach, we use the map() method on an array created from the string. Although map() is typically used for transforming elements, it can also be used for iteration.
Syntax:
array.map((element: ElementType, index: number, array: ArrayType) => {
// code
});
Example:
JavaScript
let inputStr: string = "GeeksforGeeks";
Array.from(inputStr).map(char => {
console.log(char);
});
Output:
G
e
e
k
s
f
o
r
G
e
e
k
s
Similar Reads
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 sBelow listed methods can be used to
4 min read
How to Iterate Over Characters of a String in JavaScript ? There are several methods to iterate over characters of a string in JavaScript. 1. Using for LoopThe classic for loop is one of the most common ways to iterate over a string. Here, we loop through the string by indexing each character based on the string's length.Syntaxfor (statement 1 ; statement 2
2 min read
How to Iterate over Map Elements in TypeScript ? In TypeScript, iterating over the Map elements means accessing and traversing over the key-value pairs of the Map Data Structure. The Map is nothing but the iterative interface in TypeScript. We can iterate over the Map elements in TypeScript using various approaches that include inbuilt methods and
4 min read
How to Declare an Array of Strings in TypeScript ? Arrays are fundamental data structures in TypeScript, enabling developers to manage collections of elements efficiently. Below are the approaches to declare an Array of strings in TypeScript:Table of ContentSquare Brackets NotationArray ConstructorSquare Brackets NotationUsing square brackets notati
1 min read
Delete First Character of a String in TypeScript Deleting the first character of a string in Typescript consists of removing the character at the 0th index of the string. Using various inbuilt methods, we can extract the first character of the input string and print or return only the remaining characters apart from the first character. There are
2 min read
How to Remove Spaces from a String in TypeScript ? TypeScript offers various inbuilt functions to remove spaces from a string. These functions can be used to remove spaces between characters or entire words. Below, we explore different approaches to remove spaces from a string in TypeScript.Table of ContentUsing split() and join() methodsUsing repla
4 min read
How to Iterate Over Object Properties in TypeScript In TypeScript, Objects are the fundamental data structures that use key-value pair structures to store the data efficiently. To iterate over them is a common task for manipulating or accessing the stored data. TypeScript is a superset of JavaScript and provides several ways to iterate over object pr
3 min read
How to Iterate Array of Objects in TypeScript ? In TypeScript, we can iterate over the array of objects using various inbuilt loops and higher-order functions, We can use for...of Loop, forEach method, and map method. There are several approaches in TypeScript to iterate over the array of objects which are as follows:Table of ContentUsing for...o
3 min read
How to Filter Keys of Type string[] in TypeScript ? In TypeScript, the filtering of keys of type string[] can be done by iterating over the data object, and applying the condition to the string if the key is a string. If the condition is satisfied, then an array of output results is created which consists of filtered keys of type string[]. The below
3 min read
How to Iterate over Enum Values in TypeScript? Enums in TypeScript are a way to define a set of named constants. Sometimes we may need to iterate over all the values of an enum. There are several approaches to iterate over the enum values in TypeScript which are as follows: Table of Content Using a for...in loopUsing Object.values()Using a forâ¦o
2 min read