How to Toggle String Case in .Net





5.00/5 (14 votes)
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... :)