Replacing text in a string using replacingOccurrences(of:)
Replacing Occurrences of Strings in Swift: A Unicode Trap
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โฆ


