示例#1
0
        /// <summary>
        /// Highlights one or more phrases everywhere in text by inserting it into a highlighter string.
        /// </summary>
        /// <param name="phrase">The phrase to highlight</param>
        /// <param name="highlighter">A string formatter where {0} will be replaced by the phrase (defaults to &lt;strong class="highlighted"&gt;{0}&lt;/strong&gt;)</param>
        public static string Highlight(this string text, string phrase, string highlighter="<strong class=\"highlighted\">{0}</strong>")
        {
            if (text.IndexOf(phrase, StringComparison.InvariantCultureIgnoreCase) == -1) return text;

            var replacer = new HighlightReplacer {Format = highlighter};

            return Regex.Replace(text, phrase, replacer.OnPhraseMatch, RegexOptions.IgnoreCase);
        }
示例#2
0
        /// <summary>
        /// Highlights one or more phrases everywhere in text by inserting it into a highlighter string.
        /// </summary>
        /// <param name="phrase">The phrase to highlight</param>
        /// <param name="highlighter">A string formatter where {0} will be replaced by the phrase (defaults to &lt;strong class="highlighted"&gt;{0}&lt;/strong&gt;)</param>
        public static string Highlight(this string text, string phrase, string highlighter = "<strong class=\"highlighted\">{0}</strong>")
        {
            if (text.IndexOf(phrase, StringComparison.InvariantCultureIgnoreCase) == -1)
            {
                return(text);
            }

            var replacer = new HighlightReplacer {
                Format = highlighter
            };

            return(Regex.Replace(text, phrase, replacer.OnPhraseMatch, RegexOptions.IgnoreCase));
        }