Removing emoji in a C# .NET JsonResult

I recently came across an issue at work where a JsonResult couldn't be returned because of emojis within a string.

The error I was getting looked something like this:

"Unable to translate Unicode character \\uD83C at index 485 to specified code page."

After some digging and Googling I came across a number of ways to remove emojis from a string which seems to have done the trick. The example in my case was trying to load tweets and spit them out as JSON.

var description = Regex.Replace(tweet.TextAsHtml, @"\p{Cs}", "");

The second property of the Regex.Replace is the key part—@"\p{Cs}". This string is telling the replace to remove invisible code characters, specifically one half of a surrogate pair in UTF-16 encoding in this case.

Hope this is helpful for someone!