locked
Replacing a string ignoring case, but keeping original case reference RRS feed

  • Question

  • User-195907812 posted

    Hi,

    I am creating a search results page and I generate the output in the code behind as a string.

    If the user searches for "fox", I'd like to replace the word fox with "<span class='highlight'>fox</div>".

    So "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"

    becomes "THE QUICK BROWN <span class='highlight'>fox</div> JUMPS OVER THE LAZY DOG".

    This is easily done using:

    output = Regex.Replace(output, query, "<span class='highlight'>" + query + "</span>" , RegexOptions.IgnoreCase);

    However, I'd like to ensure I keep the case of the original string when I replace it - how would I do this?

    Friday, March 29, 2019 12:57 PM

Answers

  • User475983607 posted

    I'm guessing you really want to add markup to a string not replace words.  If so, see the following.

    https://forums.asp.net/t/1677808.aspx?Highlight+matched+text+in+a+string+using+c+

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 29, 2019 1:16 PM
  • User2103319870 posted

    , I'd like to ensure I keep the case of the original string when I replace it - how would I do this?

    You could use Regex.Matches to find the exact match from sentence and then replace it like below

    string query = "fox";
    
    string output = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
    Regex rgx = new Regex(query, RegexOptions.IgnoreCase);
    MatchCollection matches = rgx.Matches(output);
    if (matches.Count > 0)
    {
    output = Regex.Replace(output, query, "<span class='highlight'>" + matches[0] + "</span>", RegexOptions.IgnoreCase);
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 29, 2019 1:21 PM

All replies

  • User475983607 posted

    I'm guessing you really want to add markup to a string not replace words.  If so, see the following.

    https://forums.asp.net/t/1677808.aspx?Highlight+matched+text+in+a+string+using+c+

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 29, 2019 1:16 PM
  • User2103319870 posted

    , I'd like to ensure I keep the case of the original string when I replace it - how would I do this?

    You could use Regex.Matches to find the exact match from sentence and then replace it like below

    string query = "fox";
    
    string output = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
    Regex rgx = new Regex(query, RegexOptions.IgnoreCase);
    MatchCollection matches = rgx.Matches(output);
    if (matches.Count > 0)
    {
    output = Regex.Replace(output, query, "<span class='highlight'>" + matches[0] + "</span>", RegexOptions.IgnoreCase);
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 29, 2019 1:21 PM
  • User-195907812 posted

    Thank you both, great solutions - I went with A2H's version due to simplicity. 

    Friday, March 29, 2019 3:15 PM