String Manipulations : Removing Substrings

Overview

Some code helper I made that is not going to be used.

The codes

Let's see what's happening here:

/// <summary>
    /// Remove substrings starting from first occurence startSubStringToRemove and ends to endSubstringtoremove with option whether the end substring should be  
    /// removed.  If any of first two parameters are null, then nothing is removed.
    /// if EndOfSubstring is null or empty, then only the substring is removed.
    /// </summary>
    /// <param name="stringToManipulate">Reference to the string which its substring is going to be removed.</param>
    /// <param name="startingSubStringToRemove">Substring to remove</param>
    /// <param name="endingSubStringToRemove">First occurence substring after the startingSubStringToRemove</param>
    /// <param name="isEndingRemoved">set whether the endingSubstring should also be removed. </param>
        private static void RemoveUnwantedSubstring(ref string stringToManipulate, string startingSubStringToRemove, string endingSubStringToRemove, bool isEndingRemoved)
        {
            if (!string.IsNullOrEmpty(stringToManipulate)
                &&
                !string.IsNullOrEmpty(startingSubStringToRemove)
                &&
                !string.IsNullOrEmpty(endingSubStringToRemove))
            {
                int substringToRemoveStartIndex =
                    stringToManipulate.IndexOfAny(startingSubStringToRemove.ToCharArray());
                // substring to remove length = index of the first occurence of endingSubStringToRemove.
                int substringToRemoveLength =
                    stringToManipulate.IndexOfAny(endingSubStringToRemove.ToCharArray(),
                                                  // find first occurence of endOfSubstring after last index of startingSubStringToRemove
                                                  stringToManipulate.LastIndexOfAny(
                                                      startingSubStringToRemove.ToCharArray()));
                if (substringToRemoveLength > 0)
                {
                    int endingSubStringToRemoveLength = isEndingRemoved ? endingSubStringToRemove.Length : 0;
                    substringToRemoveLength += startingSubStringToRemove.Length + endingSubStringToRemoveLength -
                                               substringToRemoveStartIndex - startingSubStringToRemove.Length;
                }
                else
                {
                    substringToRemoveLength = startingSubStringToRemove.Length;
                }

                stringToManipulate = stringToManipulate.Remove(substringToRemoveStartIndex, substringToRemoveLength);
            }
            else if (!string.IsNullOrEmpty(stringToManipulate)
                     &&
                     !string.IsNullOrEmpty(startingSubStringToRemove))
            {
                int substringToRemoveStartIndex =
                    stringToManipulate.IndexOfAny(startingSubStringToRemove.ToCharArray());

                stringToManipulate = stringToManipulate.Remove(substringToRemoveStartIndex,
                                                               startingSubStringToRemove.Length);
            }
        }
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License