Click here to Skip to main content
13,445,535 members (34,723 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 26 Feb 2011

Letter Case Conversion Algorithms: Title Case, Toggle Case

, 5 Mar 2011
Rate this:
Please Sign up or sign in to vote.
Algorithms extending the System.Globalization.TextInfo.ToTitleCase Method


System.Globalization namespace contains useful TextInfo.ToTitleCase() method, enabling words capitalization, i.e. title case conversion, like: "What you see is what you get" into "What You See Is What You Get". The usage is rather straightforward and takes just a single line of code:

Listing 1
string strTitleCase = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s)

where s - is the original string and strTitleCase is a new string, corresponding to the converted title case original string.

Method Extension

In case the original string contains the words, typed in all capital (Upper case) letters, then the conversion will not apply, thus the sample sentence: "HEY DUDE, THIS FOOD IS GOOD!" will stay the same. This logic prevents, for example, the acronyms/abbreviation to be converted, so "WYSIWYG" will be kept intact, presented in original format.

It would be useful to extend this built-in method with the ability to select the conversion options: convert all words, or to skip all capital lettered words. Following is the sample function, which does the job. If boolean all is set to false, then it performs just the built-in ToTitleCase(s) conversion; otherwise, it first converts all letters to Low Case using simple s.ToLower() method, and then applies the ToTitleCase(s) method:

Listing 2
public static string TitleCase(string s, bool all)
    return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(all?s.ToLower():s);

The entire solution is extremely, simple: just replacing string argument s with (all?s.ToLower():s). By applying this modified method to the original phrase "HEY DUDE, THIS FOOD IS GOOD!", it will appear as: "Hey Dude, This Food Is Good!"; thus, all words will be properly capitalized. This extended method is universally applicable to any ASCII and Unicode strings as well (beware: acronyms will be processed as well if flag all is set to true).

Test Page

Test Page is available at:[^]. On that page, notice the check box allowing to select 2 different conversion options: either to convert all words to Title Case, or to keep the all-capital-lettered words intact.

Potential Issues

Beware that both methods (original built-in and modified one) could cause some issues when dealing with math formulas, like: 2x+3y=xy could become 2X+3Y=Xy after conversion. It is possible to create another method extension to cover this issue as well, though the potential complexity and performance degradation would probably outweigh the practical advantages of such algorithm, aimed at this particular rare case issue.

Toggle Case Algotithms

Toggle Case is another useful string conversion algorithm (as FYI: such feature along with TitleCase are both included in most popular test editor, Microsoft Word). Plenty of various implementations of Toggle Case algorithms could be found at [3]; the project originated by CodeProject member Manas Bhardwaj was amended by multiple users, resulting in two most sophisticated Toggle Case algorithms (Ayoola-Bell Toggle Case Algorithm demonstrated the highest performance on selected test strings, trailed by Bell-Nedel Toggle Case Algorithm with similar performance and much simpler implementation), applicable to any ASCII/Unicode strings:

Listing 3. 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));

Listing 4. 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));


ToUpper() and ToLower()

ToUpper() and ToLower() represents two well-known and widely-used letter-case conversion functions, applicable to any ASCII and Unicode strings as well; they are included in the solutions corresponding to Listings 2, 3 and 4.


1. TextInfo.ToTitleCase Method[^]
2. How to convert string to lowercase, uppercase, or title (proper) case...[^]
3. How to Toggle String Case in .NET[^]
4. Test Page[^]


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


About the Author

Founder Infosoft International Inc
United States United States
Dr. Alexander Bell is a seasoned full-stack Software Engineer (Win/Web/Mobile). He holds PhD in EE/IT, authored 37 inventions and published 300+ technical articles. Currently focused on Java/Javascript, .NET/C#, Android/Mobile, SQL, 'Big Data', Machine Learning, AI, IoT. Alex participated in App Innovation Contests (AIC 2102/2013) with multiple winning submissions. Portfolio samples:

  1. Real-time NY Bus Tracking Web App (IoT)
  2. Android Fraction Calculator "Fractal MK-12"
  3. Android Prime Factorization Calculator 18-digit)
  4. Semaphon™ semantic phone num-to-text converter
  5. Educational Web Portal
  6. Free Online NY Payroll Tax Calculator
  7. WebTV powered by YouTube Player powered by .NET API (#1 on Google)
  8. Engineering Calculator VOLTA
  9. Big Data: Facebook and YouTube
  10. NY City Trivia Quiz
  11. Publications by A. Bell

You may also be interested in...


Comments and Discussions

GeneralRe: Manfred, This is the 2nd request to remove your unfair vote. Pin
DrABELL1-Mar-11 9:02
memberDrABELL1-Mar-11 9:02 
GeneralRe: Manfred- 1. I did not want to start this controversy: YOU we... Pin
DrABELL28-Feb-11 4:21
memberDrABELL28-Feb-11 4:21 
GeneralRe: DrABell, you are sorely wrong. I never trash anything. I'm s... Pin
Manfred R. Bihy27-Feb-11 20:50
mvpManfred R. Bihy27-Feb-11 20:50 
GeneralRe: Manfred, I am reading currently your profile info in public ... Pin
DrABELL26-Feb-11 10:10
memberDrABELL26-Feb-11 10:10 
GeneralRe: Regarding point 2.: So what! Either link to your alternates... Pin
Manfred R. Bihy26-Feb-11 9:36
mvpManfred R. Bihy26-Feb-11 9:36 
GeneralReason for my vote of 1 Your already posted this as an alter... Pin
Manfred R. Bihy26-Feb-11 8:08
mvpManfred R. Bihy26-Feb-11 8:08 
GeneralRe: Hi, Manfred 1. You are wrong: Title Case algorithm has not b... Pin
DrABELL26-Feb-11 9:19
memberDrABELL26-Feb-11 9:19 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180314.2 | Last Updated 6 Mar 2011
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid