let string = "My fantastic string"
var index = string.startIndex
while index != string.endIndex {
print(string[index])
index = index.successor()
}
Note: endIndex is after the end of the string (i.e. string[string.endIndex] is an error, but string[string.startIndex] is fine). Also, in an empty string (""), string.startIndex == string.endIndex is true. Be sure to check for empty strings, since you cannot call startIndex.successor() on an empty string.
In Swift 3, String indexes no longer have successor(), predecessor(), advancedBy(_:), advancedBy(_:limit:), or distanceTo(_:).
Instead, those operations are moved to the collection, which is now responsible for incrementing and decrementing its indices.
Available methods are .index(after:), .index(before:) and .index(_:, offsetBy:).
let string = "My fantastic string"
var currentIndex = string.startIndex
while currentIndex != string.endIndex {
print(string[currentIndex])
currentIndex = string.index(after: currentIndex)
}
Note: we're using currentIndex as a variable name to avoid confusion with the .index method.
And, for example, if you want to go the other way:
var index:String.Index? = string.endIndex.predecessor()
while index != nil {
print(string[index!])
if index != string.startIndex {
index = index.predecessor()
}
else {
index = nil
}
}
(Or you could just reverse the string first, but if you don't need to go all the way through the string you probably would prefer a method like this)
var currentIndex: String.Index? = string.index(before: string.endIndex)
while currentIndex != nil {
print(string[currentIndex!])
if currentIndex != string.startIndex {
currentIndex = string.index(before: currentIndex!)
}
else {
currentIndex = nil
}
}
Note, Index is an object type, and not an Int. You cannot access a character of string as follows:
let string = "My string"
string[2] // can't do this
string.characters[2] // and also can't do this
But you can get a specific index as follows:
index = string.startIndex.advanceBy(2)
currentIndex = string.index(string.startIndex, offsetBy: 2)
And can go backwards like this:
index = string.endIndex.advancedBy(-2)
currentIndex = string.index(string.endIndex, offsetBy: -2)
If you might exceed the string's bounds, or you want to specify a limit you can use:
index = string.startIndex.advanceBy(20, limit: string.endIndex)
currentIndex = string.index(string.startIndex, offsetBy: 20, limitedBy: string.endIndex)
Alternatively one can just iterate through the characters in a string, but this might be less useful depending on the context:
for c in string.characters {
print(c)
}