Click here to Skip to main content
15,746,759 members
Articles / Programming Languages / C#
Posted 22 Feb 2011

Tagged as


1 bookmarked

How to Toggle String Case in .NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
22 Feb 2011CPOL
Following two Toggle Case Algorithms, implemented as "pure" .NET solution (no "unsafe" coding technique, all managed code) demonstrate the best performance, tested against a variety of text strings, containing: ASCII, Unicode, all Low case, all Upper case, long numeric strings

1. Ayoola-Bell Toggle Case Algorithm
(Originally posted by Henry Ayoola, modified by Alex Bell)

/// <summary>Ayoola-Bell Toggle Case Algorithm</summary>
/// <param name="s">string</param>
/// <returns>string</returns>
public static string ToggleCase_Ayoola_Bell(string s)
    char[] chs = s.ToCharArray();
    for (int i = s.Length - 1; i >= 0; i--)
        char ch = chs[i];
        if (char.IsLetter(ch))
            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))
                chs[i] = char.IsLower(ch) ?
                         char.ToUpper(ch) :
    return (new String(chs));

The above algorithm is the fastest .NET solution; it implements the important char.IsLetter check prior to processing the text string, thus giving tremendous performance boost when converting the strings containing significant amount of non-letter characters.

Same technique is applied to the algorithm by Nedel resulted in elegant, rather simple yet effective .NET solution, trailing the first one in terms of performance: the difference is almost insignificant in case of processing the Unicode strings.

2. Bell-Nedel Toggle Case Algorithm
(Originally posted by Nedel, modified by Alex Bell)

/// <summary>Bell_Nedel Toggle Case Algorithm</summary>
/// <param name="s">string</param>
/// <returns>string</returns>
protected string ToggleCase_Bell_Nedel(string s)
    char[] charArr = s.ToCharArray();
    for (int i = 0; i < charArr.Length; ++i) {
        if (char.IsLetter(charArr[i]))
            charArr[i] = char.IsLower(charArr[i]) ?
                         char.ToUpper(charArr[i]) :
    return (new String(charArr));

Note: In terms of performance, the absolute speed champion in this "League of Extraordinary Efficient Toggle Case Algorithms" :) is the solution provided by Michael Hansen; be aware, that his solution is using "unsafe" coding technique, going beyond the boundaries of .NET managed code.




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

Written By
United States United States
Dr. Alexander Bell (aka DrABell), a seasoned full-stack Software (Win/Web/Mobile) and Data Engineer holds PhD in Electrical and Computer Engineering, authored 37 inventions and published 100+ technical articles and developed multiple award-winning apps (App Innovation Contests AIC 2012/2013 submissions) Alexander is currently focused on Microsoft Azure Cloud and .NET 6/8 development projects.

  1. HTML5/CSS3 graphic enhancement: buttons, inputs
  2. HTML5 Tables Formatting: Alternate Rows, Color Gradients, Shadows
  3. Azure web app: Engineering Calculator VOLTMATTER
  4. Azure: NYC real-time bus tracking app
  5. Quiz Engine powered by Azure cloud
  6. 'enRoute': Real-time NY City Bus Tracking Web App
  7. Advanced CSS3 Styling of HTML5 SELECT Element
  8. Aggregate Product function extends SQL
  9. YouTube™ API for ASP.NET

Comments and Discussions

GeneralYou seem to have modified my code without fully understandin... Pin
Henry.Ayoola8-Mar-11 2:17
Henry.Ayoola8-Mar-11 2:17 
You seem to have modified my code without fully understanding it (although I admit that I messed up the bracketing). The point of the check (foo == 0xdf || ch > 0xff) was to avoid calling char.IsLetter on characters which were already known not to be letters. If you're going to pull that check out then you can ditch the inner check and just have a plain else clause.

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.