Friday, May 2, 2014

A Better Solution To My charToString Method?

Oops, sorry about that. I mixed up your version with mine and didn't realize that's not quite the same thing. What I had in mind was


return new string(chars).Replace('/', '\\').Replace(' ', '-');


But yes, if you use StringBuilder then replacements are done in place, no new strings are allocated.


Though if you put everything together you probably end up with a draw:


return new StringBuilder(chars.Length).Append(chars).Replace(...).Replace(...).ToString();


again 3 objects = 2 objects for SB (itself and its internal char array) + 1 object for the final string. StringBuilder version is still in advantage because the SB object itself is smaller than the string.


No comments:

Post a Comment