Swift with Blaze

Swift with Blaze

Replacing text in a string using replacingOccurrences(of:)

Replacing Occurrences of Strings in Swift: A Unicode Trap

🅱🅻🅰🆉🅴's avatar
🅱🅻🅰🆉🅴
Jan 15, 2026
∙ Paid

String replacement in Swift looks trivial—until you touch emojis, flags, or anything outside the Unicode BMP. Then subtle Unicode details start leaking through the NSString class. Here’s a concrete example that exposes the problem.

1. The Surprising Result

let result = "🇨🇦🇺🇸".replacingOccurrences(of: "🇦🇺", with: "🇳🇮")

Result:

🇨🇳🇮🇸

At first glance…

User's avatar

Continue reading this post for free, courtesy of 🅱🅻🅰🆉🅴.

Or purchase a paid subscription.
© 2026 E.I. Blazej SLEBODA · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture