Swift - Replacing emojis in a string with whitespace
<p>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:</p>

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

<p>Because of the emojis, the url extracted from the text is <a href="http://youtu.be/SW_d3fGz1" rel="noreferrer">http://youtu.be/SW_d3fGz1</a> instead of <a href="http://youtu.be/SW_d3fGz1hk" rel="noreferrer">http://youtu.be/SW_d3fGz1hk</a>. 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).</p>

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

<pre><code>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)
}
</code></pre>

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