I'm working on an interesting project that requires some string replacement in a unusual way. Specifically, the result's case must try to match the original's case and the search itself is case insensitive.
Examples:
original: "Test Foobar Test" search for: "foobar" replace with: "helloworld" result: "Test Helloworld Test"
original: "Test FOOBAR Test" search for: "foobar" replace with: "helloworld" result: "Test HELLOWORLD Test"
Now, I realize there are many cases where this would be hard to determine (mixed case match with a different length replacement.) But what if I restricted it to three cases: All caps, all lowercase, and first letter capitalized?
So far, my plan is to do three searches: First do a case sensitive search for the all caps condition, then a case sensitive search for the first letter condition, and finally a case insensitive search and replace with all lowercase. But I'd like something faster and more elegant if possible. Any ideas?