65.9K
CodeProject is changing. Read more.
Home

How to Toggle String Case in .Net

starIconstarIconstarIconstarIconstarIcon

5.00/5 (14 votes)

Feb 9, 2011

CPOL
viewsIcon

18742

This one should outperform both the other methods for longer strings because of the StringBuilder.string s = "AbCdEfGhI§$%&/()1234567890";var sb = new StringBuilder(s.Length);foreach (char c in s) sb.Append(char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c));s =...

This one should outperform both the other methods for longer strings because of the StringBuilder.
string s = "AbCdEfGhI§$%&/()1234567890";
var sb = new StringBuilder(s.Length);
foreach (char c in s)
    sb.Append(char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c));
s = sb.ToString();
I have to admit I haven't tested it, but I'm pretty sure... :)