This is where 'Yort' praises, rants, raves and otherwise talks nonsense about Microsoft .NET and Visual Studio. Or anything else related to software development or Microsoft that he feels like throwing in. It's his blog after all...

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;