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  
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 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