Friday, June 04, 2010

Another String Function

So a reader called MD left a comment on my previous Case Insensitive String Replace post asking how he could use similar logic to find a substring, but instead of replacing it, surround it with other strings without changing the case of the original text. Specifically, he wanted to insert some kind of formatting tags around the substring to highlight it when it was displayed on screen, for use in a ‘find’ feature.

Well, this isn’t very hard at all. The basic logic for the original function went something like this;

1. Find the index of the first/next instance of the substring.

2. Add everything up to that point to the return value.

3. Add the substitute value to the return value.

4. Repeat from step 1 until no more instances found.

5. Append any remaining text after the last substring occurrence found.

This gets adjusted to;

1. Find the index of the first/next instance of the substring.

2. Add everything up to that point to the return value.

3. Add the prefix to the return value.

4. Add the portion of the original string from the index where the substring was found, up to the length of the substring (i.e the substring in it’s original case).

5. Add the suffix to the return value.

4. Repeat from step 1 until no more instances found.

5. Append any remaining text after the last substring occurrence found.

There’s a little bit of extra code to deal with situations like the substring not being found at all, or null values being passed in, but that’s basically it. Here’s a simple, verbose implementation of the code;

    static void Main(string[] args) 
    { 
      string testStr = "Microsoft Visual Studio"; 
      string newStr = EncloseSubstring(testStr, "VISUAL", "<b>", "</b>");
      newStr = EncloseSubstring(testStr, "o", "[", "]"); 
      newStr = EncloseSubstring(testStr, "Microsoft", "<b>", "</b>"); 
      newStr = EncloseSubstring(testStr, "StuDiO", "<b>", "</b>"); 
      newStr = EncloseSubstring(testStr, "fred", "<b>", "</b>"); 
    }    
    
    private static string EncloseSubstring(string originalString, string substring, string prefix, string suffix) 
    { 
      if (originalString == null) throw new ArgumentNullException("originalString"); 
      if (substring == null) throw new ArgumentNullException("substring");
      if (prefix == null) throw new ArgumentNullException("prefix"); 
      if (suffix == null) throw new ArgumentNullException("suffix"); 
      int substringStartIndex = originalString.IndexOf(substring, 0, StringComparison.OrdinalIgnoreCase);
      int substringLength = substring.Length; 
      int previousIndex = 0; 
      StringBuilder retVal = new StringBuilder(originalString.Length); 
      
      while (substringStartIndex >= 0) 
      { 
        retVal.Append(originalString.Substring(previousIndex, substringStartIndex - previousIndex)); 
        retVal.Append(prefix); 
        retVal.Append(originalString.Substring(substringStartIndex, substringLength));
        retVal.Append(suffix);
        previousIndex = substringStartIndex + substringLength; 
        substringStartIndex = originalString.IndexOf(substring, substringStartIndex + 1, StringComparison.OrdinalIgnoreCase);
      }
      if (retVal.Length > 0 && substringStartIndex < originalString.Length && substringStartIndex >= 0)
        retVal.Append(originalString.Substring(previousIndex + substringLength));
      else if (previousIndex < originalString.Length)
        retVal.Append(originalString.Substring(previousIndex));
      return retVal.ToString();
    }
 




Technorati Tags: ,,,