Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to Toggle String Case in .Net

5.00/5 (14 votes)
9 Feb 2011CPOL 18.7K  
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.

C#
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... :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)