Characters. Strings in Swift are complicated. We cannot access characters just by integers. So a for-loop over a range of Ints cannot access a string's characters.
Instead, we use the characters property on a String. This allows us to access each individual character. We can test or count or these characters.
Simple example. Let us begin with a simple for-loop. The "c" variable here is the current iteration's character. We call print() on the characters.
Detail To count the characters in a string, we can access the count property. To count specific chars, we use for-loop with if-statements.
Note With this kind of for-in loop, we cannot access adjacent characters in a string. We must use an index loop for this.
let value = "cat"// Loop over characters in string.for c in value.characters {
print(c)
}c
a
t
Count. This property returns the number of characters in the string. So with the string "carrot" it will return 6. The property cannot be assigned.
var vegetable = "carrot"// Get count of chars in string.
let c = vegetable.characters.count
print(c)6
Contains. This method receives a Character, which can be specified as a one-char string. It returns true if the Character exists in the string, and false otherwise.
var vegetable = "turnip"// The string contains this letter.
let n = vegetable.characters.contains("n")
print(n)
// There is no "x" in the string.
let x = vegetable.characters.contains("x")
print(x)true
false
Count chars. To get the number of characters in a string, we can use count. All characters, including spaces and punctuation, are counted by this property.
Tip Please see also the endIndex property. This is an index so it can be used to access and manipulate other parts of the string data.
// This string has four characters in it.
var value = "bird"
var length = value.characters.count
print(length)
// This string has six characters.// ... Spaces and other chars are all counted.
value = "x y z "
length = value.characters.count
print(length)4
6
Convert to String. A string may be created from a Character array. Here we create a Character array of six characters. When we convert it to a string, it equals the name "Sancho."
// Create a character array.
let chars: [Character] = ["S", "a", "n", "c", "h", "o"]
// Create a string from the character array.
let result = String(chars)
print(result)
// Test the string value.
if result == "Sancho" {
print(true)
}Sancho
true
Convert characters. We can apply UnicodeScalar to convert Ints into Characters. And with utf8 and utf16 we can access integral values in a string's character data.
With characters, we can combine multiple string operations. We can search for multiple things at once in a string. In one loop, we can determine if a string contains "X" or "Y."
A note. The characters property returns logical characters. In Swift, due to graphemes, some characters use more space than others. The characters property treats all characters the same.
A review. With characters, we access a property that returns all char values in a string. We can use this in a for-loop to test a string's text data.
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 Dec 30, 2021 (edit link).