If you have ever worked extensively with any kind of system that involves names, addresses or basically any kind of arena that allows users to enter data, then you might find this helpful.
Users often don’t have any concern with how things might appear within an application and there may be a few situations where you want to actually properly format some of your input. Using some of the existing methods that are available in .NET, we can handle some of the more basic casing-related needs (such as upper-casing and lower-casing a string) but your business needs may require some more complex methods to handle this.
This can be accomplished in a variety of ways, but in this post, we will leverage extension methods to help keep our code relatively clean.
Lower-casing

One of the easiest methods to handle is simply taking a string and transforming all of the existing letters into their lower-case brethren. This can easily be done using the String.ToLower() or String.ToLowerInvariant() methods as seen below:
- // Example 1: ToLowerCase (all lower-case)
- var lowercase = "This is an example".ToLowerInvariant(); // yields "this is an example"

Another commonly encountered method is the requirement to switch all of the letters within a string to their upper-case form. This is accomplished through the String.ToUpper() or String.ToUpperInvariant() method and is perfect when you need to yell at someone.
- // Example 2: ToUpperCase (all upper-case)
- var uppercase = "This is an example".ToUpperInvariant(); // yields "THIS IS AN EXAMPLE"

Title-casing is method of casing that as the name implies would focus on capitalizing the first letter of all of the words within a string. This would most commonly be used some instances involving proper names, titles and can be a bit trickier to implement that it’s upper and lower-case brethren.
Commonly, you would use the TextInfo.ToTitleCase() method to handle this as follows:
- // Example 3: ToTitleCase (upper-cases the first letter of each word)
- var nottitled = CultureInfo.InvariantCulture.TextInfo.ToTitleCase("THIS IS AN EXAMPLE");
- // Example 3a: ToTitleCase
- var nottitled = CultureInfo.InvariantCulture.TextInfo.ToTitleCase("THIS IS AN EXAMPLE".ToLowerInvariant());
- public static class StringExtensions
- {
- public static string ToTitleCase(this string s)
- {
- return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());
- }
- }
- // Example 3a: ToTitleCase (as an extension method)
- var titlecase = "THIS IS AN EXAMPLE".ToTitleCase(); / yields "This Is An Example"
- // Necessary for TextInfo (used in ToTitleCase)
- using System.Globalization;

Camel-casing is commonly used within naming conventions to denote some variables such as engineType, vogonPoetry, anotherVariableName. It is probably less commonly encountered with strings, however we will review over handling it in case the need arises. This example is specifically for space or otherwise delimited words, as you would need to add quite a bit additional logic to match the beginning of actual words within a single string.
Camel-casing is basically going to be implemented the same way as Title-casing with the exception that we will always explicitly set the first letter to lower-case as seen below:
- public static string ToCamelCase(this string s)
- {
- // Build the titlecase string
- var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());
- // Ensures that there is at-least two characters (so
- // that the Substring method doesn't freak out)
- return (titlecase.Length > 1) ? Char.ToLowerInvariant(titlecase[0]) + titlecase.Substring(1) : titlecase;
- }
- // Example 4: ToCamelCase (basically ToTitleCase except the first character is lowercased)
- var camelcase = "This is an example".ToCamelCase(); // yields this Is An Example"

In another format that you might occasionally encounter the need for capitalizing the letters that occur after apostrophes (such as in Irish or Gaelic names) which we will call "Irish-case". We will assume that this will primarily be used on names, so it will just involve them being changed into Title-case and then we will use a Regular Expression to match each of the characters that occur after an apostrophe and replace them with their upper-case variant. Prior to going into these, you’ll also need to include another namespace reference since you will be working with Regular Expressions:
- // Necessary for Irish and Custom-casing
- using System.Text.RegularExpressions;
- public static string ToIrishCase(this string s)
- {
- // This will build a Titlecased string, but
- // will uppercase any letter's that appear after
- // apostrophes (as in names)
- var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());
- // Replaces any character after an apostrophe
- // with it's uppercase variant
- return Regex.Replace(titlecase, "'(?:.)", m => m.Value.ToUpperInvariant());
- }
- // Example 5: Special Casing for names
- var irishcase = "O'reilly".ToIrishCase(); // yields "O'Reilly"
'(?:.)
To:
['-](?:.)
The expression will now match both of these characters and capitalize accordingly:
- public static string ToExtendedIrishCase(this string s)
- {
- // This will build a Titlecased string, but will
- // uppercase any letter's that appear after
- // apostrophes (as in names)
- var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());
- // Replaces any character after an apostrophe or
- // hyphen with it's uppercase variant
- return System.Text.RegularExpressions.Regex.Replace(titlecase, @"['-](?:.)", m => m.Value.ToUpperInvariant());
- }
- // Example 5a: ToExtendedIrishCase (like Irishcase,
- // but it also captializes characters after hyphens as well)
- var irishcase = "kathleen hely-hutchinson".ToIrishCase(); // yields "Kathleen Hely-Hutchinson"
Custom-Casing (provides the lower-cased version of a string with only the characters specified being upper-cased)

Custom-casing would be a method that might function as a starting-point if you wanted to write your own completely custom form of casing if the need arises. In this example of Custom-casing, the method is going to transition all of the characters to lower-case variants and then it will use an expression to match only the characters specified and change them into upper-case ones:
- public static string ToCustomCasing(this string s, string[] characters)
- {
- // If there are no characters to specifically capitalize,
- // return the initial string
- if (characters == null || !characters.Any())
- {
- return s;
- }
- // Replacement expression
- var replacements = String.Format("[{0}]", String.Join("", characters).ToLowerInvariant());
- // Replaces any characters that were passed in
- return System.Text.RegularExpressions.Regex.Replace(s.ToLowerInvariant(), replacements, m => m.Value.ToUpperInvariant());
- }
- //Example 6: Custom Casing (only upper-cases specific characters)
- var customcase = "This is an example".ToCustomCasing(new string[] { "e", "i" }); // yields "thIs Is an ExamplE"
These are just a few methods that might help you solve some tricky issues or implement specific conventions when dealing with user or otherwise generated input within your applications. The use of extension methods provides a very clean approach and the methods themselves are quite straight-forward and should be easy to extend if a specific need arises.
You can download all of the examples that were specified within this post in both C# and Visual Basic from the following repository on GitHub:
Download all of these Casing Examples and Extension Methods in C# and Visual Basic from GitHub.

Santhakumar MunuswamyPosted Nov 11, 2015, 8:07 AM
Good one
Gowtham KPosted Nov 7, 2015, 10:14 AM
Thanks for sharing
Former memberPosted Nov 7, 2015, 9:37 AM
Nice One
Manoj KulkarniPosted Nov 7, 2015, 7:09 AM
Nice article. Thank you for sharing
Sibeesh VenuPosted Nov 7, 2015, 3:33 AM
Nice Share
Harshad PansuriyaPosted Nov 6, 2015, 11:29 PM
Nice one