Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

How to Toggle String Case in .NET

By , 15 Feb 2011
Rate this:
Please Sign up or sign in to vote.
This does the same. (I'm not sure it's better, but I'm a fan of this style) Smile | :)
static string toggle(string t)
    return (from c in t select new string(Char.IsLower(c) ? Char.ToUpper(c) : Char.ToLower(c), 1)).Aggregate((a, b) => a += b);
much better performance than stringbuilder below
static string ToggleCaseByCeChode(string s)
     char[] chars = s.ToCharArray();
     for (int i = 0; i < chars.Length; i++)
          else if (chars[i] >122) { }
          else if (chars[i] > 90 && chars[i]<97) { }
          else if ((chars[i] & 32) == 32)
               chars[i] = (char)(chars[i] - 32);
               chars[i] = (char)(chars[i] | 32);
     return new String(chars);


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

About the Author


United States United States
No Biography provided

Comments and Discussions

GeneralReason for my vote of 5 I love the compact. PinmemberLongTTH22-Feb-11 3:43 
Reason for my vote of 5
I love the compact.
GeneralReason for my vote of 5 VERY NICE Pinmembergokhan_ertastr14-Feb-11 20:03 
General"Clickable" link to the test page: &lt;a href=" PinmemberDrABELL10-Feb-11 7:59 
GeneralI've built the test page to compare two Algorithms, provided... PinmemberDrABELL10-Feb-11 7:35 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 15 Feb 2011
Article Copyright 2011 by cechode
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid