C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
For: We next use a while-loop on the string's characters. We begin at startIndex (this is like zero but a string index).
EndIndex: We terminate the loop when the index equals the endIndex. So we continue until the last char index.
Index: We use this func to increment the current index through the string. We cannot just increment it like an Int, as it is an index.
Finally: We print out the individual characters in the string. We must access a string by an index, not an Int.
Swift program that uses string length, endIndex, for-loop
let name = "dotnetCodex"
// Get the length of the string.
let length = name.endIndex
print("Length = \(length)")
// Loop over all characters in a string.
// ... We start at startIndex.
// ... And proceed until the endIndex (the length).
var temp = name.startIndex;
var i = 0;
while (temp != name.endIndex) {
print("\(i) = \(name[temp])")
temp = name.index(after: temp);
i += 1;
}
Output
Length = Index(_base: Swift.String.UnicodeScalarView.Index(
_position: 11), _countUTF16: 0)
0 = d
1 = o
2 = t
3 = n
4 = e
5 = t
6 = p
7 = e
8 = r
9 = l
10 = s
Swift program that uses characters.count, tests length
let platform = "Apple"
// Get length of string.
let count = platform.characters.count
// See if string is 5 characters long.
if count == 5 {
print(true)
}
// Print count.
print(count)
Output
true
5
Then: It invokes remove() to eliminate the final char. The string "abcd" is now just "abc."
Swift program that removes char based on string length
var value = "abcd"
print(value)
// The last index is before endIndex.
let lastCharIndex = value.index(before: value.endIndex)
// Remove last char.
value.remove(at: lastCharIndex)
print(value)
Output
abcd
abc
Then: We print all chars in reverse order. We continue calling index() until no more may exist, and then we exit the loop.
PrintSwift program that loops over string chars in reverse order
let name = "clovis"
// Begin at the last valid index.
// ... This is endIndex's predecessor.
var index = name.index(before: name.endIndex)
while true {
print(name[index])
// Reduce index by 1 if still greater than 0.
// ... Otherwise break out of loop.
if index > name.startIndex {
index = name.index(before: index)
}
else {
break
}
}
Output
s
i
v
o
l
c
Swift program that reduces length of string
import Foundation
let title = "Divine Comedy"
// Get index for the desired length.
let desiredLength = title.index(title.startIndex, offsetBy: 6)
// Use substring.
// ... Reduce length of string and create new substring.
let truncated = title.substring(to: desiredLength)
print(truncated)
Output
Divine
Instead: We must access strings with an index. This is an abstraction for accessing string data.
Tip: Once we understand that chars and strings must be accessed through indexes (and never Ints), strings are more manageable in Swift.
Although: The added complexity of indexes does tend to lead to more complex code to do simple string manipulation.