Click here to Skip to main content
15,892,222 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.
0.00/5 (No votes)
24 Feb 2011CPOL 5.8K   1   1
What about this :) string ToggleStringCase(string strToToggle){ StringBuilder sb = new StringBuilder(); foreach(char ch in strToToggle.ToCharArray()) { if((int)ch>=65&&(int)ch<91) { sb.Append((char)((int)ch+32)); } else...
What about this :)
string ToggleStringCase(string strToToggle)
{
   StringBuilder sb = new StringBuilder();
   foreach(char ch in strToToggle.ToCharArray())
   {
      if((int)ch>=65&&(int)ch<91)
      {
        sb.Append((char)((int)ch+32));
      }
      else if((int)ch>=97&&(int)ch<123)
      {
        sb.Append((char)((int)ch-32))
      }
      else
      {
        sb.Append(ch);
      }
    }
   return sb.ToString();
}

License

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


Written By
Architect
India India
• An Architect, developer, trainer, and student
• 10 years of experience in Architecture and Development of enterprise applications
• Extensive knowledge in designing and developing Web-based enterprise applications using open source and Microsoft Technologies
• Extensive experience in Angular, AngularJS, ReactJs, NodeJs, JavaScript, ASP.Net, C#, CSS, HTML

Comments and Discussions

 
GeneralThis version is similar to the Alternate 1 posted by Robert ... Pin
DrABELL25-Feb-11 13:57
DrABELL25-Feb-11 13:57 
This version is similar to the Alternate 1 posted by Robert Rohde: so far, his solution is the most elegant one, plus it works on ASCII and Unicode as well.

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.