Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Pascal and Camel Case to Display Text Conversion

0.00/5 (No votes)
8 Jun 2011 1  
Pascal and Camel Case to display text conversion

Introduction


This is a handy function that you can use if you are using Reflection and have a property name, for example, "myPropertName" and you want it to be a label or a heading formatted like "My Property Name".


Background


Sometimes I use Reflection to create columns in basic administration functions in an application where there is no real business logic behind the objects. Things like type and status for example.


Using the code


Pretty straightforward. Just send a property name to it like myPropertyName or MyPropertyName and it will turn it into "My Property Name".


C#
private string createDisplayText(string propertyName)
{
    StringBuilder builder = new StringBuilder();

    if(!string.IsNullOrEmpty(propertName))
    {
        character = char.ToUpper(propertName[0]);
        builder.Append(character)
    }
    for (int i = 1; i < propertyName.Length; i++)
    {
        char character = propertyName[i];
        if (char.IsUpper(character))
        {
            builder.Append(" ");
        }
    }
    return builder.ToString();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here