Home
Map
String LengthGet the length of a string with the count property. Loop over the chars based on indexes.
Swift
This page was last reviewed on Aug 22, 2023.
String length. A 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.
Next We use a while-loop on the string's characters. We begin at startIndex (this is like zero but a string index).
Finally We terminate the loop when the index equals the endIndex. So we continue until the last char index.
Info 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.
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
Get char based on length. This 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.
Then It invokes 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
Loop, reverse order. 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.
Then We print all chars in reverse order. We continue calling index() until no more may exist, and then we exit the loop.
print
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
Reduce string length. A string's length cannot be directly modified. But with a string slice expression we can truncate a string. Conceptually this reduces the length.
String Substring
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
Some comments. 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.
Info 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.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 22, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.