Inspired by a question in the CodeProject's ASP.NET forum, I decided to quick-assemble some code for displaying emoticons within a web page.
Displaying emoticons is the task of replacing text-fragments within a string (e.g. ":-)", ":-(" ) by some HTML code that displays an image (e.g. and ). So simple spoken, the whole project is a nicely encapsulated string-replace that hopefully is useful to the guy who asked the question, as well as to other programmers.
The project contains a ready-to-compile-and-run Visual Studio 2003 solution with one Web Form web page Index.aspx to test out the functionality. The effective code is contained within the class
Emoticon in the file Emoticons.cs.
Emoticon has two
static members. The first is:
/// Returns a collection of all available emoticons.
public static Emoticon All
to get a collection of all available emoticons. Within this member, the actual references to the image files are created, so if you want to add your own emoticons, you must place the news images within the Emoticons subfolder and modify this member to "know" the new emoticons.
static member is the function:
/// Returns a string with all emoticons replaced by their images.
public static string Format( string input )
This member function does the core replacement of an input string with the HTML-code to display the the emoticons. Any occurrence of the emoticons registered within
All are replaced and returned by this function.
Using the code
To use the code, simply call the
Format function like this:
string text = Emoticon.Format( "Some text with a smiley :-) emoticon." );
- 2003-06-29: Article created