Click here to Skip to main content
15,881,600 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.
0.00/5 (No votes)
14 Jun 2011CPOL 9.5K   1   2
This version treats the input string as an indexed string. The code is a little shorter.private string createDisplayText(string propertyName){ if (String.IsNullOrEmpty(propertyName)) { return String.Empty; } StringBuilder builder = new StringBuilder(); ...

This version treats the input string as an indexed string. The code is a little shorter.


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

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

    for (int i = 1; i < propertyName.Length; ++i)
    {
        builder.Append((char.IsUpper(propertyName, i)) ? " " + 
                propertyName[i].ToString() : propertyName[i].ToString());
    }
    return builder.ToString();
}

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) SouthData, Inc.
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

 
GeneralI had tried that and propertyName[i] was returning me the de... Pin
Steve Roberson16-Jun-11 3:17
Steve Roberson16-Jun-11 3:17 
GeneralYou can remove the ToString() calls if you replace the " " w... Pin
kiswa0016-Jun-11 2:23
kiswa0016-Jun-11 2:23 

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.