Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / C#
Tip/Trick

How to Toggle String Case in .NET

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

C#
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:
C#
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)


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
GeneralRe: you have a strange idea of "optimal"... Pin
Seishin#9-Dec-11 0:16
Seishin#9-Dec-11 0:16 
GeneralReason for my vote of 3 Neat idea, poorly implemented. Appen... Pin
AspDotNetDev9-Feb-11 14:01
protectorAspDotNetDev9-Feb-11 14:01 
GeneralAs FYI: High Resolution timer added to the test page Pin
DrABELL25-Feb-11 13:40
DrABELL25-Feb-11 13:40 
GeneralSuggestion: to post a summary of this discussion, highlighting the best Algorithms Pin
DrABELL24-Feb-11 14:32
DrABELL24-Feb-11 14:32 
GeneralRe: Suggestion: to post a summary of this discussion, highlighting the best Algorithms Pin
Manas Bhardwaj25-Feb-11 0:52
professionalManas Bhardwaj25-Feb-11 0:52 
QuestionWhy? Pin
Andrew Rissing10-Feb-11 3:57
Andrew Rissing10-Feb-11 3:57 
AnswerRe: Why? Pin
Manas Bhardwaj10-Feb-11 4:12
professionalManas Bhardwaj10-Feb-11 4:12 
GeneralRe: Why? Toggle Case option is included in MS Word, it's a valuable feature Pin
DrABELL24-Feb-11 4:19
DrABELL24-Feb-11 4:19 
AnswerRe: Why? Pin
DrABELL14-Feb-11 16:25
DrABELL14-Feb-11 16:25 
GeneralRe: Why? Pin
Andrew Rissing15-Feb-11 4:27
Andrew Rissing15-Feb-11 4:27 
GeneralRe: Why? Pin
DrABELL15-Feb-11 8:46
DrABELL15-Feb-11 8:46 
GeneralRe: Why? Pin
damnedyankee24-Feb-11 1:27
damnedyankee24-Feb-11 1:27 
GeneralRe: Why? Pin
DrABELL24-Feb-11 2:22
DrABELL24-Feb-11 2:22 
GeneralRe: Why? Pin
damnedyankee24-Feb-11 3:34
damnedyankee24-Feb-11 3:34 
GeneralRe: Why? Pin
DrABELL24-Feb-11 4:11
DrABELL24-Feb-11 4:11 
GeneralRe: Why? Pin
damnedyankee24-Feb-11 5:34
damnedyankee24-Feb-11 5:34 
GeneralRe: Why? Pin
DrABELL24-Feb-11 6:52
DrABELL24-Feb-11 6: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.