C# Extension Methods

C#.NET comes with a lot of string processing methods like Substring, Compare, IndexOf, Replace, etc. Although the number and scope of such methods are vast, programmers still constantly have to rewrite similar text processing methods over and over again to suit their needs. Luckily we can extend on them and create all kinds of advanced string methods in C#.
public string Replace(string oldChar, string newChar)
public string Replace(string oldString, string newString)
We will focus on Replace method here. In C#, Replace method has the following two signatures: Replace method does not give us the flexibility to replace after/from a specified position in a string or to replace for a specified number of occurrences within the string. This was possible in VB6. So, especially in migration projects there arises a need where we need to have extension methods of Replace. The signature of the extension methods we will create are as follows:
public string Replace(string oldString, string newString, int startIndex, int Count)
public string Replace(string oldString, string newString)
startIndex: This is the position in the string from/after which ‘oldString’ should be replaced with ‘newString’. Count: The number of occurrences of ‘oldString’ to be replaced starting from ‘startIndex’.

Method 1


public string Replace(string oldString, string newString, int startIndex)
This method is used to replace ‘oldString’ with ‘newString’ inside the original string. The catch here is that all replace operations take place only after a certain position in the original string indicated by ‘startIndex’.
public static string Replace (this string expressionString, string oldString, string newString, int startIndex) {
    int iCount = 0;
    StringBuilder sToFind = new StringBuilder ();
    if (startIndex & lt; = expressionString.Length - 1) {
        Regex exp;
        while (iCount != oldString.Length) {
            sToFind.Append ("[");
            sToFind.Append (Regex.Escape (Convert.ToString (oldString[iCount])));
            sToFind.Append ("]");
            iCount++;
        }
        exp = new Regex (Convert.ToString (sToFind), RegexOptions.IgnoreCase);
        if (!exp.IsMatch (expressionString)) {
            return expressionString.Substring (startIndex, expressionString.Length - startIndex);
        } else {
            string StringReplace;
            StringReplace = exp.Replace (expressionString, newString, -1, startIndex);
            return StringReplace;
        }
    } else {
        return string.Empty;
    }
}

Method 2


public string Replace(string oldString, string newString, int startIndex)
This method is used to replace ‘oldString’ with ‘newString’ inside the original string. In this case, not only do all replace operations take place after a certain position in the original string indicated by ‘startIndex’, the number of occurrences of ‘oldString’ replaced with ‘newString’ are limited to a fixed number as specified by ‘Count’.
public static string Replace (this string expressionString, string oldString, string newString, int startIndex, int Count) {
    int iCount = 0;
    StringBuilder sToFind = new StringBuilder ();
    if (startIndex & lt; = expressionString.Length - 1) {
        Regex exp;
        while (iCount != oldString.Length) {
            sToFind.Append ("[");
            sToFind.Append (Regex.Escape (Convert.ToString (oldString[iCount])));
            sToFind.Append ("]");
            iCount++;
        }
        exp = new Regex (Convert.ToString (sToFind), RegexOptions.IgnoreCase);
        if (!exp.IsMatch (expressionString)) {
            return expressionString.Substring (startIndex, expressionString.Length - startIndex);
        } else {
            string StringReplace;
            StringReplace = exp.Replace (expressionString, newString, Count, startIndex);
            return StringReplace;
        }
    } else {
        return string.Empty;
    }

}

Note

  1. In the second method containing count, if the count is passed as ‘-1’, it will replace all occurrences after ‘startIndex’. This behavior would be similar to the first method.
  2. The above methods are defined as extension methods. However, their definitions can be changed and they may also be used as custom methods within code.

Comments

Popular posts from this blog

Automate Import of Functions/WebAPI in Azure API Management as backend and using OpenAPI definition and Terraform

Transcripts with Microsoft Bot Framework v4

Managing built-in cache in Azure API Management