I want to remove all underscores from the uppercase string of the character following the underscore. So, for example: _my_string_becomes: MyStringsimilarly: my_stringbecomesMyString
Is there an easier way to do this? I currently have the following (if no input has two consecutive underscores):
StringBuilder sb = new StringBuilder();
int i;
for (i = 0; i < input.Length - 1; i++)
{
if (input[i] == '_')
sb.Append(char.ToUpper(input[++i]));
else if (i == 0)
sb.Append(char.ToUpper(input[i]));
else
sb.Append(input[i]);
}
if (i < input.Length && input[i] != '_')
sb.Append(input[i]);
return sb.ToString();
Now I know that this is not completely related, but I decided to run some numbers in the implementations presented in the answers, and here are the results in milliseconds for each implementation using the 1000000 iterationsstring "_my_string_121_a_"::
Achilles: 313
Raj: 870
Damian: 7916
Dmitry: 5380
Equalsk: 574
Method used:
Stopwatch stp = new Stopwatch();
stp.Start();
for (int i = 0; i < 1000000; i++)
{
sb = Test("_my_string_121_a_");
}
stp.Stop();
long timeConsumed= stp.ElapsedMilliseconds;
In the end, I think I will go with the Raj implementation, because it is just very simple and easy to understand.