Strings & Unicode — graphemes, indices, substrings, APIs
Understand Unicode-aware Strings: extended grapheme clusters, String.Index, substrings, common APIs (prefix/suffix/contains, split/join), and copy-on-write performance.
Strings & Unicode — graphemes, indices, substrings, APIs is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.
Unicode & graphemes
Swift String is Unicode-correct. One user-visible character may be multiple scalars (an extended grapheme cluster).
- Examples: emoji with skin tones, accented letters
- Count uses characters, not bytes
Counting characters
count measures characters (grapheme clusters), not bytes.
let wave = "👋🏽" // hand + skin tone modifier
let text = "Hi " + wave
print(text, "count =", text.count) // counts visible charactersIndices & navigation
Use String.Index with helpers like startIndex, index(after:), index(_:offsetBy:).
let s = "Swift"
let firstIndex = s.startIndex
let secondIndex = s.index(after: firstIndex)
print(s[firstIndex], s[secondIndex]) // S wSubstrings
Substring views share storage with the original String. Convert to String(left) for long-term storage.
let phrase = "hello.world"
let dot = phrase.firstIndex(of: ".")!
let left = phrase[..<dot] // Substring (shares storage)
let right = phrase[phrase.index(after: dot)...] // Substring
print(left, right)Common APIs
Handy APIs: hasPrefix, hasSuffix, contains, plus split/joined.
let path = "/usr/local/bin"
print(path.hasPrefix("/usr")) // true
print(path.hasSuffix("bin")) // true
print(path.contains("local")) // true
let parts = path.split(separator: "/") // [ "", "usr", "local", "bin" ] without first slash
let joined = parts.joined(separator: "-")
print(joined)Performance (COW)
Performance: Strings and Substrings use copy‑on‑write. Copies are cheap until modified. Promote Substring to String for long-term storage.
String index rules
Quick check: What is the correct way to index a Swift String?
Recap
Recap: Unicode-aware Strings use grapheme clusters. Navigate with String.Index, slice as Substring, use common APIs, and remember COW performance.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Strings & Unicode — graphemes, indices, substrings, APIs” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Strings & Unicode — graphemes, indices, substrings, APIs”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.
What will I learn in “Strings & Unicode — graphemes, indices, substrings, APIs”?
Understand Unicode-aware Strings: extended grapheme clusters, String.Index, substrings, common APIs (prefix/suffix/contains, split/join), and copy-on-write performance. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
How long does the “Strings & Unicode — graphemes, indices, substrings, APIs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Strings & Unicode — graphemes, indices, substrings, APIs
- Common APIs — prefix/suffix/contains, split/join
- Performance notes — copy-on-write