Wednesday, January 14, 2009

Case Insensitive String Replace

This post is a modification from Rick Strahl's Web Log, to replace string. ignoring case. The code has been modified to suit C# .NET 3.5 coding style - with extension methods.

Recently in one of the project I was working on, I came across a problem where I needed to replace parts of the string, while preserving the case, otherwise. That last part is the problem. While it's possible to simply call string.ToLower(), it'd result in the whole string converted to lowercase, regardles of whether they match the replacement criteria or otherwise.

The code below is designed to address the issue. It's almost copy and paste, except you need to rename the namespace to that of your solution. Reference the namespace and class, and string.Replace() will have 2 extra overloads.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YourNameSpace.SubNameSpace
{
public static class StringExtension
{
/// <summary>
/// String replace function that support
/// </summary>
/// <param name="OrigString">Original input string</param>
/// <param name="FindString">The string that is to be replaced</param>
/// <param name="ReplaceWith">The replacement string</param>
/// <param name="Instance">Instance of the FindString that is to be found. if Instance = -1 all are replaced</param>
/// <param name="CaseInsensitive">Case insensitivity flag</param>
/// <returns>updated string or original string if no matches</returns>
public static string Replace(this string OrigString, string FindString,
string ReplaceWith, int Instance,
bool CaseInsensitive)
{
if (Instance == -1)
return OrigString.Replace(FindString, ReplaceWith, CaseInsensitive);

int at1 = 0;
for (int x = 0; x < Instance; x++)
{

if (CaseInsensitive)
at1 = OrigString.IndexOf(FindString, at1, OrigString.Length - at1, StringComparison.OrdinalIgnoreCase);
else
at1 = OrigString.IndexOf(FindString, at1);

if (at1 == -1)
return OrigString;

if (x < Instance - 1)
at1 += FindString.Length;
}

return OrigString.Substring(0, at1) + ReplaceWith + OrigString.Substring(at1 + FindString.Length);
}


/// <summary>
/// Replaces a substring within a string with another substring with optional case sensitivity turned off.
/// </summary>
/// <param name="OrigString">String to do replacements on</param>
/// <param name="FindString">The string to find</param>
/// <param name="ReplaceString">The string to replace found string wiht</param>
/// <param name="CaseInsensitive">If true case insensitive search is performed</param>
/// <returns>updated string or original string if no matches</returns>
public static string Replace(this string OrigString, string FindString,
string ReplaceString, bool CaseInsensitive)
{
int at1 = 0;
while (true)
{
if (CaseInsensitive)
at1 = OrigString.IndexOf(FindString, at1, OrigString.Length - at1, StringComparison.OrdinalIgnoreCase);
else
at1 = OrigString.IndexOf(FindString, at1);

if (at1 == -1)
return OrigString;

OrigString = OrigString.Substring(0, at1) + ReplaceString + OrigString.Substring(at1 + FindString.Length);

at1 += ReplaceString.Length;
}

return OrigString;
}
}
}

No comments:

Post a Comment