Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

<< Back to SWIFT

Swift Characters: String Examples

Use the characters property on strings to access individual chars. Loop over characters with a for-loop.
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.

Count: 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.

Swift program that uses characters property let value = "cat" // Loop over characters in string. for c in value.characters { print(c) } Output 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.
Swift program that uses count to get string length var vegetable = "carrot" // Get count of chars in string. let c = vegetable.characters.count print(c) Output 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.
Swift program that uses contains to search for chars 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) Output 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.

endIndex: Length
Swift program that uses count, gets string length // 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) Output 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."
Swift program that converts Character array to string // 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) } Output 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.Convert Int to Character
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.
The Dev Codes