- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift Program to Iterate through each character of the string
In Swift, we can iterate through each character of the string very easily. For the iteration, Swift provides the following methods −
Using for-in loop
Using forEach() function
Using enumerated() function
Method 1: Using for-in loop
We can use a for-in loop for the iteration. It iterates through each character of the string and then performs the expressions given inside the loop body or can display them on the output screen.
Syntax
for x in mstr{ // body }
Here mstr is the string and x stores the current character from the string in each iteration.
Example
In the following Swift program, we will iterate through each character of the string. So first we will create a string and then we use a for-in loop to iterate through each character and display the output on the output screen.
import Foundation import Glibc let InputString = "This is Swift Tutorial" print("Characters in the current string: \(InputString) are:") for char in InputString { print(char) }
Output
Characters in the current string: This is Swift Tutorial are: T h i s i s S w i f t T u t o r i a l
Method 2: Using forEach() method
We can also use a forEach() method for the iteration. It calls the specified closure on each character of the given string and displays the output.
Syntax
mStr.forEach{mBody}
Here mStr is the string and mBody is a closure which takes each character from the given string as a parameter and returns the result according to the expression present in the closure.
Example
In the following Swift program, we will iterate through each character of the string. So first we will create a string and then we pass a closure in the forEach() method to iterate through each character of the input string and then display the output on the output screen.
import Foundation import Glibc let InputString = "This is Swift 7 Tutorial" print("Characters in the current string: \(InputString) are:") InputString.forEach{C in print(C)}
Output
Characters in the current string: This is Swift 7 Tutorial are: T h i s i s S w i f t 7 T u t o r i a l
Method 3: Using enumerated() method
We can also use an enumerated() method for the iteration. It generally returns a sequence of pairs(m, n), where m represents the index and n represents its associated character. Or we can say that using the enumerated() method you can get the character as well as its associated index value.
Syntax
for(idx, char) in mStr.enumerated(){ // Body }
Here mStr is the string and mBody is the body of the loop. The idx represents the index and the char represents the current character.
Example
In the following Swift program, we will iterate through each character of the string. So first we will create a string and then we use the enumerated() method to iterate through each character and its corresponding index and then display the output.
import Foundation import Glibc let InputString = "Swift Tutorial" print("Characters in the current string: \(InputString) are:") for (idx, char) in InputString.enumerated() { print("Character = \(char) and index = \(idx)") }
Output
Characters in the current string: Swift Tutorial are: Character = S and index = 0 Character = w and index = 1 Character = i and index = 2 Character = f and index = 3 Character = t and index = 4 Character = and index = 5 Character = T and index = 6 Character = u and index = 7 Character = t and index = 8 Character = o and index = 9 Character = r and index = 10 Character = i and index = 11 Character = a and index = 12 Character = l and index = 13
Conclusion
So this is how we can iterate through each character of the string. Where all three methods work effectively. Each method provides different functionalities such as the for-in loop provides better control over the iteration using return, continue, break, etc., statements whereas using the forEach() function you can take advantage of closures and using the enumerated() method you can access the index of the characters.