Optimizing some string manipulation - C#