Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

How to Toggle String Case in .Net

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
9 Feb 2011CPOL 16.9K   9   6
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)


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 Agree with AspDotNetDev...Nice Pin
thatraja5-Mar-11 3:50
professionalthatraja5-Mar-11 3:50 
GeneralReason for my vote of 5 simple Pin
LongTTH22-Feb-11 3:42
LongTTH22-Feb-11 3:42 
GeneralReason for my vote of 5 this is more optimal Pin
shadi_abushaar13-Feb-11 1:54
shadi_abushaar13-Feb-11 1:54 
GeneralReason for my vote of 5 nice and compact Pin
vishaljindal11-Feb-11 2:19
vishaljindal11-Feb-11 2:19 
GeneralReason for my vote of 5 I have found that this algorithm pro... Pin
DrABELL10-Feb-11 7:43
DrABELL10-Feb-11 7:43 
GeneralReason for my vote of 5 Of all that different versions so fa... Pin
AspDotNetDev9-Feb-11 14:00
protectorAspDotNetDev9-Feb-11 14:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.