Click here to Skip to main content
12,247,901 members (57,230 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3K views
1 bookmarked
Posted

How to Toggle String Case in .NET

, 22 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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 (refer to the test...
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 (refer to the test site: http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx[^])

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) :
                         char.ToLower(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]) :
                         char.ToLower(charArr[i]);
        }
    }
    return (new String(charArr));
}
//*********************************************************

Note: In terms of performance, the absolute speed champion in this "League of Extraordinary Efficient Toggle Case Algorithms" Smile | :) 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.

License

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

Share

About the Author

DrABELL
President Infosoft International Inc
United States United States
Dr. A. Bell is a full-stack software developer (Win/Web/Mobile). He holds PhD in EE/IT, published 300+ articles, authored 37 inventions and is credited for 10+ Enterprise level projects; currently focused on HTML5/CSS3, Javascript/jQuery, .NET/WPF/C#, Android/Angular.js, 'Big Data', AI, IoT. Alex participated in App Innovation Contests (AIC 2102/2013) with multiple winning submissions. Sample projects/pubs:
  1. Real-time NY Bus monitoring app (IoT)
  2. Semaphon™ semantic phone num-to-text converter
  3. Educational Web Portal
  4. Free Online NY Payroll Tax Calculator
  5. WebTV powered by Embedded YouTube Player (Goog #1 YouTube API for ASP.NET)
  6. Top-50 Digital Cameras (powered by iMark-DCAM rating engine)
  7. Pure CSS3 Slide Show
  8. Inflation Calculator
  9. CSS3 Modal Pop-up Dialog
  10. Multilingual Geocoder with Interactive Map
  11. Online Semantic Analyzer (Concordance Calculator)
  12. NY City Job Market and Agency Ratings
  13. Advanced CSS3 Table Formatting

You may also be interested in...

Comments and Discussions

 
GeneralYou seem to have modified my code without fully understandin... Pin
Henry.Ayoola8-Mar-11 3:17
memberHenry.Ayoola8-Mar-11 3:17 

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
Web02 | 2.8.160426.1 | Last Updated 23 Feb 2011
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid