Tuesday 14 August 2012

Searching Words from Paragraph

Following method is helpful when we have to search specific word or words separated by specific symbol (here space and ,). This will highlight those words using CSS.

[Code]

public string HighlightKeywords(string input, string keywords)
    {
        if (input == string.Empty || keywords == null)
        {
            return input;
        }
        char[] splt = { ' ', ',' };
        string[] sKeywords = keywords.Split(splt);
        foreach (string sKeyword in sKeywords)
        {
            try
            {

// here highlight is a class in CSS.

                input = Regex.Replace(input, sKeyword, string.Format("<span class=\"highlight\">{0}</span>", "$0"), RegexOptions.IgnoreCase);
            }
            catch
            {
                //
            }
        }
        return input;
    }

//CSS class

.highlight
{
    background-color:Yellow;
    font-style:italic;
}

[/Code]

No comments:

Post a Comment