Posted By

hungheroic on 10/22/10


Tagged

search highlight keyword


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

wallybh
sharathreddyp
franksmile0310
Tyster
jalalx
ovuncgursoy


Highlight keyword in string


 / Published in: C#
 

  1. public string HighlightKeywords(string input, string keywords)
  2. {
  3. if (input == string.Empty || keywords == string.Empty)
  4. {
  5. return input;
  6. }
  7.  
  8. var keywordChar = keywords.Split(' ');
  9. foreach (var keyword in keywordChar)
  10. {
  11. try
  12. {
  13. input = Regex.Replace(input, keyword, string.Format("<span class=\"highlight\">{0}</span>", "$0"), RegexOptions.IgnoreCase);
  14. }
  15. catch (Exception)
  16. {
  17. // Log the error.
  18. }
  19. }
  20.  
  21. return input;
  22. }

Report this snippet  

You need to login to post a comment.