Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Detect and remove Emoticons from Text on Windows Phone

DZone's Guide to

Detect and remove Emoticons from Text on Windows Phone

· Mobile Zone
Free Resource

Get gorgeous, multi-touch charts for your iOS application with just a few lines of code.

In my recent project, I work with a lot of text that get’s its value from user input. To enable the best possible experience for my users, I choose the InputScope Text on all TextBoxes, because it provides the word suggestions while writing.

The Text will be submitted to a webserver via a REST API. And now the problem starts. The Emoticons that are part of the Windows Phone OS are not supported by the API and the webserver.

Of course, I was immediately looking for a way to get around this. I thought this might be helpful for some of you, so I am sharing two little helper methods for detecting and removing the Emoticons.

Here is my detecting method, that returns a bool after checking the text (input):

public static bool HasUnsoppertedCharacter(string text)
             {
                string pattern = @"[.{allemoticonshere}]";

                Regex RegexEmoticonsKeyboard = new Regex(pattern);

                bool booleanreturnvalue = false;

                if (RegexEmoticonsKeyboard.IsMatch(text))
                {
                    booleanreturnvalue = true;
                }
                else if (!RegexEmoticonsKeyboard.IsMatch(text))
                {
                    booleanreturnvalue = false;
                }
                return booleanreturnvalue;
            }

As you can see, I declared a character range with all Emoticons. If one or more Emoticons is found, the bool will always return true. This can be used to display a MessageBox for example while the user is typing.

The second method quietly removes the Emoticon from any text that is passed as input string.

 public static string RemovedUnSoppertedCharacterString(string text)
             {
                 string result = string.Empty;
                 string cleanedResult = string.Empty;
                 string pattern = @"[.{allemoticonshere}]";

                 MatchCollection matches = Regex.Matches(text, pattern);

                 foreach (Match match in matches)
                 {
                     result = Regex.Replace(text, pattern, string.Empty);
                     cleanedResult = Regex.Replace(result, "  ", " ");
                 }
                 return cleanedResult;
             }

Also here I am using the character range with all Emoticons. The method writes all occurrences of Emoticons into a MatchCollection for Regex. I iterate trough this collection to remove all of them. The Method also checks the string for double spaces in the text and makes it a single space, as this happens while removing the emoticons. Use this method with care, as it could be seen as a data loss from your users.

You might have notices that there is a placeholder “{allemoticonshere}” in the code above. WordPress or the code plugin I use aren’t supporting the Emoticons in code, that’s why I attached my helper class.

As always, I hope this will be helpful for some of you.

Happy coding!

.Net developers: use Highcharts, the industry's leading interactive charting library, without writing a single line of JavaScript.

Topics:

Published at DZone with permission of Marco Siccardi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}