Click here to Skip to main content
11,924,573 members (57,972 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked

How to Toggle String Case in .NET

, 14 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Shows how to toggle the contents of a string from upper case to lower, or vice versa
The following snippet shows how to toggle string casing in .NET:

public string ToggleCase(string input)
    string result = string.Empty;
    char[] inputArray = input.ToCharArray();
    foreach (char c in inputArray)
        if (char.IsLower(c))
            result += c.ToString().ToUpper();
        else if (char.IsUpper(c))
            result += c.ToString().ToLower();
            result += c.ToString();
    return result;

This code snippet could be simplified as follows:
public string ToggleCase(string input)
    string result = string.Empty;
    char[] inputArray = input.ToCharArray();
    foreach (char c in inputArray)
        if (char.IsLower(c))
            result += Char.ToUpper(c);
            result += Char.ToLower(c);
    return result;

But still performance of Alternate 1 and Alternate 2 is 2...3 times better than of this one: see the online comparative test at:[^]

Alex Bell


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


About the Author

Manas Bhardwaj
Netherlands Netherlands
Read my personal blog at

You may also be interested in...

Comments and Discussions

GeneralRe: 1. Thanks for your note: I typically use "Add comment" becau... Pin
DrABELL23-Feb-11 16:23
memberDrABELL23-Feb-11 16:23 
1. Thanks for your note: I typically use "Add comment" because another option is a pop-up, which appears only on mouse hover (not always the case, cuz there is no obvious reason to move the mouse over some others commentsSmile | :)
2. I would agree with you on analytical performance assessments of variable algorithms, though some numeric estimates are handy. Tested on pure ASCII test strings (see the test site link) the performance boost by using Ayoola-Bell or Bell-Nedel (fastest ones) algorithms is almost 10:1; tested on Unicode it's about 6:1. In other words, pretty significant performance improvement!
My best, Alex
GeneralReason for my vote of 1 result = instead of StringBuilder Pin
Selvin9-Dec-11 6:57
memberSelvin9-Dec-11 6:57 
GeneralAre you going to add the other alternatives to your webinfoc... Pin
SimmoTech24-Mar-11 0:06
memberSimmoTech24-Mar-11 0:06 
GeneralGot the highest number of alternatives.. I have not seen cod... Pin
Pravin Patil, Mumbai8-Mar-11 23:47
memberPravin Patil, Mumbai8-Mar-11 23:47 
GeneralTwo fastest Toggle Case Algorithms (Ayoola-Bell and Bell-Ned... Pin
DrABELL23-Feb-11 10:01
memberDrABELL23-Feb-11 10:01 
GeneralRe: I know, I was just saying that those faster solutions do not... Pin
AspDotNetDev23-Feb-11 13:51
protectorAspDotNetDev23-Feb-11 13:51 
GeneralReason for my vote of 5 I am giving my vote of 5 because the... Pin
DrABELL14-Feb-11 18:29
memberDrABELL14-Feb-11 18:29 
GeneralRe: Actually, the performance can be increased by a multiple of ... Pin
AspDotNetDev23-Feb-11 8:57
protectorAspDotNetDev23-Feb-11 8:57 
General[edit] made variable names more C#-ish. (no vote from me, b... Pin
Indivara14-Feb-11 15:08
subeditorIndivara14-Feb-11 15:08 
GeneralReason for my vote of 5 the way to toggle the string is opti... Pin
shadi_abushaar13-Feb-11 2:54
membershadi_abushaar13-Feb-11 2:54 
GeneralRe: you have a strange idea of "optimal"... Pin
Seishin#9-Dec-11 1:16
memberSeishin#9-Dec-11 1:16 
GeneralReason for my vote of 3 Neat idea, poorly implemented. Appen... Pin
AspDotNetDev9-Feb-11 15:01
protectorAspDotNetDev9-Feb-11 15:01 
GeneralAs FYI: High Resolution timer added to the test page Pin
DrABELL25-Feb-11 14:40
memberDrABELL25-Feb-11 14:40 
GeneralSuggestion: to post a summary of this discussion, highlighting the best Algorithms Pin
DrABELL24-Feb-11 15:32
memberDrABELL24-Feb-11 15:32 
GeneralRe: Suggestion: to post a summary of this discussion, highlighting the best Algorithms Pin
Manas Bhardwaj25-Feb-11 1:52
memberManas Bhardwaj25-Feb-11 1:52 
QuestionWhy? Pin
Andrew Rissing10-Feb-11 4:57
memberAndrew Rissing10-Feb-11 4:57 
AnswerRe: Why? Pin
Manas Bhardwaj10-Feb-11 5:12
memberManas Bhardwaj10-Feb-11 5:12 
GeneralRe: Why? Toggle Case option is included in MS Word, it's a valuable feature Pin
DrABELL24-Feb-11 5:19
memberDrABELL24-Feb-11 5:19 
AnswerRe: Why? Pin
DrABELL14-Feb-11 17:25
memberDrABELL14-Feb-11 17:25 
GeneralRe: Why? Pin
Andrew Rissing15-Feb-11 5:27
memberAndrew Rissing15-Feb-11 5:27 
GeneralRe: Why? Pin
DrABELL15-Feb-11 9:46
memberDrABELL15-Feb-11 9:46 
GeneralRe: Why? Pin
damnedyankee24-Feb-11 2:27
memberdamnedyankee24-Feb-11 2:27 
GeneralRe: Why? Pin
DrABELL24-Feb-11 3:22
memberDrABELL24-Feb-11 3:22 
GeneralRe: Why? Pin
damnedyankee24-Feb-11 4:34
memberdamnedyankee24-Feb-11 4:34 
GeneralRe: Why? Pin
DrABELL24-Feb-11 5:11
memberDrABELL24-Feb-11 5:11 
GeneralRe: Why? Pin
damnedyankee24-Feb-11 6:34
memberdamnedyankee24-Feb-11 6:34 
GeneralRe: Why? Pin
DrABELL24-Feb-11 7:52
memberDrABELL24-Feb-11 7:52 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.3 | Last Updated 15 Feb 2011
Article Copyright 2011 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid