Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

How to Toggle String Case in .NET

, 14 Feb 2011
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();
        else
            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);
        else
            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: http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx[^]
 
Regards,
Alex Bell

License

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

Share

About the Author

Manas Bhardwaj
Architect
Netherlands Netherlands
Read my personal blog at www.manasbhardwaj.net.
Follow on   Twitter

Comments and Discussions

 
GeneralRe: 1. Thanks for your note: I typically use "Add comment" becau... PinmemberDrABELL23-Feb-11 15:23 
GeneralReason for my vote of 1 result = instead of StringBuilder PinmemberSelvin9-Dec-11 5:57 
GeneralAre you going to add the other alternatives to your webinfoc... PinmemberSimmoTech23-Mar-11 23:06 
GeneralGot the highest number of alternatives.. I have not seen cod... PinmemberPravin Patil, Mumbai8-Mar-11 22:47 
GeneralTwo fastest Toggle Case Algorithms (Ayoola-Bell and Bell-Ned... PinmemberDrABELL23-Feb-11 9:01 
GeneralRe: I know, I was just saying that those faster solutions do not... PinprotectorAspDotNetDev23-Feb-11 12:51 
GeneralReason for my vote of 5 I am giving my vote of 5 because the... PinmemberDrABELL14-Feb-11 17:29 
GeneralRe: Actually, the performance can be increased by a multiple of ... PinprotectorAspDotNetDev23-Feb-11 7:57 
General[edit] made variable names more C#-ish. (no vote from me, b... PinsubeditorIndivara14-Feb-11 14:08 
GeneralReason for my vote of 5 the way to toggle the string is opti... Pinmembershadi_abushaar13-Feb-11 1:54 
GeneralRe: you have a strange idea of "optimal"... PinmemberSeishin#9-Dec-11 0:16 
GeneralReason for my vote of 3 Neat idea, poorly implemented. Appen... PinprotectorAspDotNetDev9-Feb-11 14:01 
GeneralAs FYI: High Resolution timer added to the test page PinmemberDrABELL25-Feb-11 13:40 
GeneralSuggestion: to post a summary of this discussion, highlighting the best Algorithms PinmemberDrABELL24-Feb-11 14:32 
Hi everybody,
 
As this post (Toggle Case Algorithms) and the following discussion thread became excessively lengthy and hardly readable, then I would suggest to publish a separate tip, describing the Toggle Case algorithms, which are implemented as "pure" .NET solutions (managed code written in C#) and demonstrate the best performance, namely: Ayoola-Bell and Bell-Nedel (see the Alternate 13).
 
In regards to the possible "unsafe" solution (using pointers), capable of processing the ASCII/Unicode string, the best "tested" performer so far is presented in Alternate 11 by Michael Hansen (bug corrected by Alex Bell).
 
Special thanks go to the project originator Manas Bhardwaj, who started this tip, even though his solution was found less efficient that the other listed above
 
Thanks and regards,
Alex
 
PS. As a reminder, test page is available online at: http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx[^]
GeneralRe: Suggestion: to post a summary of this discussion, highlighting the best Algorithms PinmemberManas Bhardwaj25-Feb-11 0:52 
QuestionWhy? PinmemberAndrew Rissing10-Feb-11 3:57 
AnswerRe: Why? PinmemberManas Bhardwaj10-Feb-11 4:12 
GeneralRe: Why? Toggle Case option is included in MS Word, it's a valuable feature PinmemberDrABELL24-Feb-11 4:19 
AnswerRe: Why? PinmemberDrABELL14-Feb-11 16:25 
GeneralRe: Why? PinmemberAndrew Rissing15-Feb-11 4:27 
GeneralRe: Why? PinmemberDrABELL15-Feb-11 8:46 
GeneralRe: Why? Pinmemberdamnedyankee24-Feb-11 1:27 
GeneralRe: Why? PinmemberDrABELL24-Feb-11 2:22 
GeneralRe: Why? Pinmemberdamnedyankee24-Feb-11 3:34 
GeneralRe: Why? PinmemberDrABELL24-Feb-11 4:11 

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
Web02 | 2.8.140916.1 | Last Updated 15 Feb 2011
Article Copyright 2011 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid