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