NEW: Master Swift design patterns with my latest book! >>

How to loop through letters in a string

Written by Paul Hudson    @twostraws

You can loop through every character in a string by treating it as an array. Thanks to Swift's extended support for international languages and emoji, this works great no matter what kind of language you're using.

This code prints out each character one at a time:

let str = "sunday, monday, happy days"
for char in str {
    print("Found character: \(char)")
}

Available from iOS 7.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Become a Swift guru

I wrote a book called Pro Swift that's dedicated to helping you master the Swift language, and it includes seven hours of video demonstrating each technique!

Click here to visit the Hacking with Swift store >>