Apr 07
2006
2006
Implementing Emoticons in C#

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | public string Emotify(string inputText) { #region Create Emote hashtable Hashtable htEmotes = new System.Collections.Hashtable(100); htEmotes.Add(":))", "21"); htEmotes.Add(":)>-", "67"); htEmotes.Add(":)", "1"); htEmotes.Add(":-)", "1"); htEmotes.Add(":((", "20"); htEmotes.Add(":(", "2"); // Add other Yahoo emotes #endregion StringBuilder sb = new StringBuilder(inputText.Length); for (int i = 0; i < inputText.Length; i++) { string strEmote = string.Empty; foreach (string emote in htEmotes.Keys) { if (inputText.Length - i >= emote.Length && emote.Equals(inputText.Substring(i, emote.Length), StringComparison.InvariantCultureIgnoreCase)) { strEmote = emote; break; } } if (strEmote.Length != 0) { sb.AppendFormat("<img src='images/{0}.gif' alt='{1}' />", htEmotes[strEmote], strEmote); i += strEmote.Length - 1; } else { sb.Append(inputText[i]); } } return sb.ToString(); } |


