Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

String replace while ignoring casing

Modified: 2009/12/31 13:10 by Vincevanm - Categorized as: ASP.NET, Csharp
An easy way to replace or add certain characters around a string when you have no control over the input casing (for example a searchterm) is by using "Regex.Replace" with "RegexOptions.IgnoreCase" enabled instead of a "string.Replace".

This way the string to be replaced (searchterm) can be written like "Searchterm" or "searchTerm" and the replacement will always take place.

Example:
string toBeReplaced, replacedWith;
string ogSearchString = currentUrl.Parameters "searchstring";


toBeReplaced = string.Format( " < c 0 > {0} < / c 0>", ogSearchString);
replacedWith = string.Format("< strong > {0} < /strong >", ogSearchString);


resultList = Regex.Replace(resultList"HitHighlightedSummary".ToString(), toBeReplaced, replacedWith, RegexOptions.IgnoreCase);

 © Evident Interactive BV