Swift Strings
A String is a piece of text — a sequence of Character values. Swift strings are fully Unicode-aware: accented letters, Chinese characters and emoji all count as single characters.
Creating strings
let greeting = "Hello"
var empty = ""
let alsoEmpty = String()
let repeated = String(repeating: "ab", count: 3)
print(greeting)
print(empty.isEmpty, alsoEmpty.isEmpty)
print(repeated)
empty += "now filled"
print(empty)Hello
true true
ababab
now filled
Strings declared with var can be changed; let strings cannot.
Length and characters
count returns the number of characters. Loop over a string to get each character:
let word = "naïve"
print(word.count)
for ch in word {
print(ch, terminator: "|")
}
print()
print(Array(word))5
n|a|ï|v|e|
["n", "a", "ï", "v", "e"]
Joining strings
let first = "Ada"
let last = "Lovelace"
var full = first + " " + last
full.append("!")
print(full)
let parts = ["2026", "10", "16"]
print(parts.joined(separator: "-"))Ada Lovelace!
2026-10-16
Useful string methods
import Foundation
let title = " Learning Swift Step by Step "
let clean = title.trimmingCharacters(in: .whitespaces)
print(clean)
print(clean.uppercased())
print(clean.lowercased())
print(clean.hasPrefix("Learning"), clean.hasSuffix("Step"))
print(clean.contains("Swift"))
print(clean.replacingOccurrences(of: "Step", with: "Stage"))
print(clean.split(separator: " "))Learning Swift Step by Step
LEARNING SWIFT STEP BY STEP
learning swift step by step
true true
true
Learning Swift Stage by Stage
["Learning", "Swift", "Step", "by", "Step"]
| Method | Returns |
|---|---|
uppercased() / lowercased() |
a new string in upper / lower case |
hasPrefix(_:) / hasSuffix(_:) |
whether the string starts / ends with the text |
contains(_:) |
whether the text appears anywhere |
split(separator:) |
an array of substrings |
replacingOccurrences(of:with:) |
a new string with replacements (needs import Foundation) |
trimmingCharacters(in:) |
the string without leading/trailing characters (needs Foundation) |
Accessing characters by position
Because characters can have different sizes in memory, Swift does not allow word[2] with an integer. You use String.Index values instead, or the convenient first, last, prefix and suffix:
let language = "Swift"
print(language.first!, language.last!)
print(language.prefix(3))
print(language.suffix(2))
let third = language.index(language.startIndex, offsetBy: 2)
print(language[third])
print(language[language.startIndex..<third])S t
Swi
ft
i
Sw
Tip: If you need many random-access lookups, convert once with
Array(text)and index the array.
Comparing strings
== compares the text character by character, and is aware of Unicode equivalence: "é" written as one scalar equals "e" followed by a combining accent.
let a = "café"
let b = "cafe\u{301}"
print(a == b)
print(a.count, b.count)
print("Apple" == "apple")
print("Apple".lowercased() == "apple")true
4 4
false
true
Special characters
| Escape | Meaning |
|---|---|
\n |
new line |
\t |
tab |
\" |
double quote |
\\ |
backslash |
\u{1F600} |
any Unicode scalar by its hex code |
Test yourself
What does "héllo".count return?
- 5
- 6
- 7
- It depends on the encoding
Why can't you write text[0] on a Swift String?
- Strings are immutable
- Characters have variable size, so Swift uses
String.Indexinstead of Int positions - You must use
text.charAt(0) - Index 0 is reserved
Which method returns ["a", "b", "c"] for "a,b,c"?
- explode(",")
- split(separator: ",")
- chars()
- joined(",")