Click here to Skip to main content
11,565,743 members (39,402 online)
Click here to Skip to main content

Tagged as

How to Toggle String Case in .NET

, 8 Mar 2011 CPOL 1.4K 2
Rate this:
Please Sign up or sign in to vote.
Several of the answers here lack fallbacks for Unicode.static string ToggleCaseHA(string str) { char[] chs = str.ToCharArray(); for (int i = chs.Length - 1; i >= 0; i--) { char ch = chs[i]; char foo = (char)(ch & ~0x20); if ((foo >= 0x41 && foo <= 0x5a)...
Several of the answers here lack fallbacks for Unicode.

static string ToggleCaseHA(string str) {
    char[] chs = str.ToCharArray();
    for (int i = chs.Length - 1; i >= 0; i--) {
        char ch = chs[i];
        char foo = (char)(ch & ~0x20);
        if ((foo >= 0x41 && foo <= 0x5a) || (foo >= 0xc0 && foo <= 0xde && foo != 0xd7))
            chs[i] = (char)(ch ^ 0x20);
        else if (foo == 0xdf || (ch > 0xff && char.IsLetter(ch))
            chs[i] = char.IsLower(ch) ? char.ToUpper(ch) : char.ToLower(ch);
    }
    return new string(chs);
}

Note by Alex Bell: this algorithm was included in test set at: http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx[^] .

This algorithm indeed can process the Unicode String, but so can do all other algorithms (Alternate 1, Alternate 2, etc.) except for the "TogglebyCeChode" (which is the fastest, but does not work on Unicode text).

After sample testing, this proposed algorithm demonstrated just a marginal performance increase over Alternate 1 and Alternate 2.

License

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

Share

About the Author

Henry.Ayoola
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralFurther testing of this Algorithms lead to interesting findi... Pin
DrABELL16-Feb-11 7:05
memberDrABELL16-Feb-11 7:05 

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 | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 8 Mar 2011
Article Copyright 2011 by Henry.Ayoola
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid