Swift String – Loop over Every Character
To loop or iterate over every character in a String in Swift, we can use for-in statement with String. During each iteration we get the access to this character in the loop body.
The syntax to iterate over every character ch
in String str
is
</>
Copy
for ch in str {
//code
}
Example
In the following program, we will take a string and iterate over its every character using for-in statement.
main.swift
var str = "Hello World"
for ch in str {
print(ch)
}
Output
Conclusion
In this Swift Tutorial, we learned how to loop over every character in given String in Swift programming.