Search code examples
stringswiftunicodeemoji

Swift - Replacing emojis in a string with whitespace


I have a method that detects urls in a string and returns me both the urls and the ranges where they can be found. Everything works perfectly until there are emojis on the string. For example:

"I'm gonna do this callenge as soon as I can swing again 😂😂😂\n http://youtu.be/SW_d3fGz1hk"

Because of the emojis, the url extracted from the text is http://youtu.be/SW_d3fGz1 instead of http://youtu.be/SW_d3fGz1hk. I figured that the easiest solution was to just replace the emojis on the string with whitespace characters (cause I need the range to be correct for some text styling stuff). Problem is, this is extremely hard to accomplish with Swift (most likely my abilities with the Swift String API is lacking).

I've been trying to do it like this but it seems that I cannot create a string from an array of unicode points:

var emojilessStringWithSubstitution: String {
    let emojiRanges = [0x1F601...0x1F64F, 0x2702...0x27B0]
    let emojiSet = Set(emojiRanges.flatten())
    let codePoints: [UnicodeScalar] = self.unicodeScalars.map {
        if emojiSet.contains(Int($0.value)) {
            return UnicodeScalar(32)
        }
        return $0
    }
    return String(codePoints)
}

Am I approaching this problem the wrong way? Is replacing emojis the best solution here? If so, how can I do it?


Solution

  • You can use pattern matching (for emoji patterns) to filter out emoji characters from your String.

    extension String {
    
        var emojilessStringWithSubstitution: String {
            let emojiPatterns = [UnicodeScalar(0x1F601)...UnicodeScalar(0x1F64F),
                                 UnicodeScalar(0x2702)...UnicodeScalar(0x27B0)]
            return self.unicodeScalars
                .filter { ucScalar in !(emojiPatterns.contains{ $0 ~= ucScalar }) }
                .reduce("") { $0 + String($1) }
        }  
    }
    
    /* example usage */
    let str = "I'm gonna do this callenge as soon as I can swing again 😂😂😂\n http://youtu.be/SW_d3fGz1hk"
    print(str.emojilessStringWithSubstitution)
    
    /* I'm gonna do this callenge as soon as I can swing again
       http://youtu.be/SW_d3fGz1hk */
    

    Note that the above only makes use of the emoji intervals as presented in your question, and is in no way representative for all emojis, but the method is general and can swiftly be extended by including additional emoji intervals to the emojiPatterns array.


    I realize reading your question again that you'd prefer substituting emojis with whitespace characters, rather than removing them (which the above filtering solution does). We can achieve this by replacing the .filter operation above with a conditional return .map operation instead, much like in your question

    extension String {
    
        var emojilessStringWithSubstitution: String {
            let emojiPatterns = [UnicodeScalar(0x1F600)...UnicodeScalar(0x1F64F),
                             UnicodeScalar(0x1F300)...UnicodeScalar(0x1F5FF),
                             UnicodeScalar(0x1F680)...UnicodeScalar(0x1F6FF),
                             UnicodeScalar(0x2600)...UnicodeScalar(0x26FF),
                             UnicodeScalar(0x2700)...UnicodeScalar(0x27BF),
                             UnicodeScalar(0xFE00)...UnicodeScalar(0xFE0F)]
    
            return self.unicodeScalars
                .map { ucScalar in
                    emojiPatterns.contains{ $0 ~= ucScalar } ? UnicodeScalar(32) : ucScalar }
                .reduce("") { $0 + String($1) }
        }
    }
    

    I the above, the existing emoji intervals has been extended, as per your comment to this post (listing these intervals), such that the emoji check is now possibly exhaustive.