Return to Snippet

Revision: 34448
at October 22, 2010 13:48 by hungheroic


Initial Code
public string HighlightKeywords(string input, string keywords)
        {
            if (input == string.Empty || keywords == string.Empty)
            {
                return input;
            }

            var keywordChar = keywords.Split(' ');
            foreach (var keyword in keywordChar)
            {
                try
                {
                    input = Regex.Replace(input, keyword, string.Format("<span class=\"highlight\">{0}</span>", "$0"), RegexOptions.IgnoreCase);
                }
                catch (Exception)
                {
                    // Log the error.
                }
            }

            return input;
        }

Initial URL

                                

Initial Description

                                

Initial Title
Highlight keyword in string

Initial Tags
search

Initial Language
C#