Search code examples
swiftswift5

Split utf16 string with special characters using delimiter


I want to split this utf-16 string in Swift 5

ddd¾̷̱̲͈́͌͠ͰͿΔδόϡϫЍа

delimiter : "¾"

I've tried the following codes

let Arr =  "ddd¾̷̱̲͈́͌͠ͰͿΔδόϡϫЍа".split{$0 == "¾"}.map(String.init)

let Arr = "ddd¾̷̱̲͈́͌͠ͰͿΔδόϡϫЍа".components(separatedBy: "¾")

but both failed


Solution

  • The Element of String is Character. A Character is an extended grapheme cluster, which means it composes all combining characters. The Character in this String is ¾̷̱̲͈́͌͠, so when you try to split on ¾, it's not found.

    I believe what you're trying to operate on is UnicodeScalars, which are individual code points. To do that, you need to first call .unicodeScalars:

    let arr = "ddd¾̷̱̲͈́͌͠ͰͿΔδόϡϫЍа".unicodeScalars.split(separator: "¾").map(String.init)
    // ["ddd", "̷̱̲͈́͌͠ͰͿΔδόϡϫЍа"]
    

    Note that the string you've posted here is UTF-8, not UTF-16. Swift can't operate directly on UTF-16 literals (you typically store them as Data or [UInt16] and then convert them to String). I don't believe this changes your question, however.