0Pricing
Swift Academy · Lesson

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 — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 characters

Indices & 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 w

Substrings

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.

Frequently asked questions

Is the “Strings & Unicode — graphemes, indices, substrings, APIs” lesson free?

Yes — the full text of “Strings & Unicode — graphemes, indices, substrings, APIs” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

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

  1. Strings & Unicode — graphemes, indices, substrings, APIs
  2. Common APIs — prefix/suffix/contains, split/join
  3. Performance notes — copy-on-write
← Back to Swift Academy