String
lengthA Swift string
contains a certain number of characters. Chars use varying amounts of memory. So we must access our string
through char
indexes.
With count, a property, we get the number of characters. And EndIndex
returns the index after the final index in a string
. We can access chars based on startIndex
and endIndex
.
Count
To get the length of a string
we can use the count property. We can test this value against an Int
. Here we detect a string
with 5 chars.
let platform = "Apple" // Get length of string. let count = platform.count // See if string is 5 characters long. if count == 5 { print(true) } // Print count. print(count)true 5
EndIndex
Here we introduce a constant string
with contents "dotnetperls." It has 11 characters. The endIndex
property equals the value 11. We print it out.
while
-loop on the string
's characters. We begin at startIndex
(this is like zero but a string
index).endIndex
. So we continue until the last char
index.index()
func
to increment the current index through the string
. We cannot just increment it like an Int
.string
by an index, not an Int
.let name = "dotnetperls" // 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; }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
char
based on lengthThis example uses the endIndex
to access the last char
in a string
. It calls index()
with "before" on endIndex
which just reduces the index value by 1 char
.
remove()
to eliminate the final char
. The string
"abcd" is now just "abc."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)abcd abc
Sometimes we want to loop over a string
's chars in reverse order. Here we use endIndex
and call index()
with "before" to get the last character's index.
index()
until no more may exist, and then we exit the loop.let name = "swift" // 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 } }t f i w s
string
lengthA string
's length cannot be directly modified. But with a string
slice expression we can truncate a string
. Conceptually this reduces the length.
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[..<desiredLength] print(truncated)Divine
Strings in Swift are not like strings in many other computer languages. Each char
may have a different size. So we cannot treat all chars the same.
string
data.The easiest way to get the character count in a string
is by using the count property. We can also access endIndex
, and index()
can get the last char
in a string
.