Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Pascal and Camel Case to Display Text Conversion

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
8 Jun 2011CPOL 15.1K   2   10
Why not take care of the first character before the loop, instead of checking it every time (not that it makes a big difference on modern hardware)? This also moves the char declaration out of the loop.private string createDisplayText(string propertyName){ if...
Why not take care of the first character before the loop, instead of checking it every time (not that it makes a big difference on modern hardware)? This also moves the char declaration out of the loop.

private string createDisplayText(string propertyName)
{
    if (String.IsNullOrEmpty(propertyName))
    {
        return String.Empty;
    }

    StringBuilder builder = new StringBuilder();
    char character = char.ToUpper(propertyName[0]);
    builder.Append(character);

    for (int i = 1; i < propertyName.Length; ++i)
    {
        character = propertyName[i];
        if (char.IsUpper(character))
        {
            builder.Append(" ");
        }
        builder.Append(character);
    }

    return builder.ToString();
}


This isn't really an alternate, as much as a minor modification.

Also, the for loop could be shorter, if somewhat less readable.
for (int i = 1; i < propertyName.Length; ++i)
{
    character = propertyName[i];
    builder.Append((char.IsUpper(character)) ? ' ' + character : character);
}

License

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


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAdditional note: this algorithm could be further extended by... Pin
DrABELL16-Jun-11 2:15
DrABELL16-Jun-11 2:15 
GeneralReason for my vote of 5 Practical and elegant solution, 5*! ... Pin
DrABELL16-Jun-11 2:06
DrABELL16-Jun-11 2:06 
GeneralReason for my vote of 5 Good suggestion. Pin
Ben Kotvis8-Jun-11 2:51
Ben Kotvis8-Jun-11 2:51 
GeneralI just modified it to add the check if the string is empty, ... Pin
Ben Kotvis8-Jun-11 2:51
Ben Kotvis8-Jun-11 2:51 
GeneralRe: Thanks, I'm glad you found my suggestions useful! Pin
kiswa008-Jun-11 3:16
kiswa008-Jun-11 3:16 
GeneralReason for my vote of 4 nice addition need to check for stri... Pin
Tim Yen7-Jun-11 20:04
Tim Yen7-Jun-11 20:04 
GeneralRe: I meant to add that in, thanks for reminding me! Pin
kiswa008-Jun-11 3:01
kiswa008-Jun-11 3:01 
GeneralI guess the only concern I have with this is that it will th... Pin
Ben Kotvis7-Jun-11 3:17
Ben Kotvis7-Jun-11 3:17 
GeneralRe: Yeah, I forgot to include the check for null or empty. It's ... Pin
kiswa008-Jun-11 3:05
kiswa008-Jun-11 3:05 
GeneralThat is a good idea! Thanks for the suggestion. Pin
Ben Kotvis7-Jun-11 2:43
Ben Kotvis7-Jun-11 2:43 

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.