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

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.

This example uses string extensions to convert strings between these three formats.

The following code shows the ToPascalCase string extension.

// 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;
}

The code splits the string into words separated by spaces. It then loops through the words, capitalizing them and adding them together.

The following code shows the ToCamelCase string extension.

// 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 is similar to the ToPascalCase extension except it doesn't capitalize the first word.

The following code shows the ToProperCase string extension.

// 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).

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.