Convert between Pascal case, camel case, and proper case in C#

example

This example uses string extension methods to convert strings between Pascal case, camel case, and proper case.

In Pascal case, each word is capitalized as in ThisStringIsPascalCased. In camel case, each word except the first is capitalized as in thisStringIsCamelCased. In proper case, each word is capitalized and separated by spaces as in This String Is Proper Cased.

The following code shows the ToPascalCase string extension method.

// Convert the string to Pascal case.
public static string ToPascalCase(this string the_string)
{
    // If there are 0 or 1 characters, just return the string.
    if (the_string == null) return the_string;
    if (the_string.Length < 2) return the_string.ToUpper();

    // Split the string into words.
    string[] words = the_string.Split(
        new char[] { },
        StringSplitOptions.RemoveEmptyEntries);

    // Combine the words.
    string result = "";
    foreach (string word in words)
    {
        result +=
            word.Substring(0, 1).ToUpper() +
            word.Substring(1);
    }

    return result;
}

This method splits the string into words separated by spaces. It then loops through the words, capitalizing them and combining them.

The following code shows the ToCamelCase string extension method.

// Convert the string to camel case.
public static string ToCamelCase(this string the_string)
{
    // If there are 0 or 1 characters, just return the string.
    if (the_string == null || the_string.Length < 2)
        return the_string;

    // Split the string into words.
    string[] words = the_string.Split(
        new char[] { },
        StringSplitOptions.RemoveEmptyEntries);

    // Combine the words.
    string result = words[0].ToLower();
    for (int i = 1; i < words.Length; i++)
    {
        result +=
            words[i].Substring(0, 1).ToUpper() +
            words[i].Substring(1);
    }

    return result;
}

The ToCamelCase extension method is similar to the ToPascalCase method except it doesn’t capitalize the first word.

The following code shows the ToProperCase extension method.

// Capitalize the first character and add a space before
// each capitalized letter (except the first character).
public static string ToProperCase(this string the_string)
{
    // If there are 0 or 1 characters, just return the string.
    if (the_string == null) return the_string;
    if (the_string.Length < 2) return the_string.ToUpper();

    // Start with the first character.
    string result = the_string.Substring(0, 1).ToUpper();

    // Add the remaining characters.
    for (int i = 1; i < the_string.Length; i++)
    {
        if (char.IsUpper(the_string[i])) result += " ";
        result += the_string[i];
    }

    return result;
}

ToProperCase loops through the characters in the string and inserts a space in front of each capital letter. Notice how it uses char.IsUpper to determine whether a character is upper case.

Note that this code does not handle multi-character abbreviations (such as USSenator).


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in extension methods, strings and tagged , , , , , , , , , . Bookmark the permalink.

5 Responses to Convert between Pascal case, camel case, and proper case in C#

  1. Pingback: Convert between Pascal case, camel case, and proper case

  2. Mark says:

    Fantastic! I just used this in a project to reformat file names imported into Unity3d. Thanks very much.

  3. Anonymous says:

    Thanks, very useful.

    I suggest a minor improvement in readability though:

    foreach(string word in words)
    {
        result +=
            word.Substring(0, 1).ToUpper() +
            word.Substring(1);
    }

    it’s clearer to understand the following:

    foreach(string word in words)
    {
        result += char.ToUpper(word[0]);
        result += word.Substring(1);
    }
  4. ToCamelCase does not work if the string is a Pascal case string. It converts every character to lower case which is not how a Web API camel case converter works.

    • RodStephens says:

      That’s not how this example is supposed to work. It converts a string containing spaces into camel case. You can easily change it to work the same way as the API converter if you like.

Leave a Reply

Your email address will not be published. Required fields are marked *