Click here to Skip to main content
12,243,180 members (22,008 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6K views
1 bookmarked
Posted

Pascal and Camel Case to Display Text Conversion

, 8 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

kiswa00
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralThat is a good idea! Thanks for the suggestion. Pin
Ben Kotvis7-Jun-11 3:43
memberBen Kotvis7-Jun-11 3: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 8 Jun 2011
Article Copyright 2011 by kiswa00
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid