Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
Delete First Character of a String in TypeScript
Next article icon

How to Iterate Over Characters of a String in TypeScript ?

Last Updated : 25 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Table of Content

  • Using for Loop
  • Using forEach() method
  • Using split() method
  • Using for...of Loop
  • Using map() Method

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

Next Article
Delete First Character of a String in TypeScript

G

gauravggeeksforgeeks
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • TypeScript

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. Syntax for (statement 1 ; statement
    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 Content Square Brackets NotationArray ConstructorSquare Brackets NotationUsing square brackets nota
    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 Content Using split() and join() methodsUsing rep
    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 Content Using for..
    4 min read
  • How to Extract Interface Members in TypeScript ?
    In TypeScript, you can extract interface members (properties and methods) from a class using several approaches. we are going to learn how to extract interface members in TypeScript. Below are the approaches used to extract interface members in TypeScript: Table of Content Manual Extractionimplement
    3 min read
  • How to Cast Object to Interface in TypeScript ?
    In TypeScript, sometimes you need to cast an object into an interface to perform some tasks. There are many ways available in TypeScript that can be used to cast an object into an interface as listed below: Table of Content Using the angle bracket syntaxUsing the as keywordUsing the spread operatorU
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences