[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: 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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.