Handling Casing Issues Though Extension Methods In C# And Visual Basic

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.


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:

  1. // Example 1: ToLowerCase (all lower-case)  
  2. 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.
  1. // Example 2: ToUpperCase (all upper-case)  
  2. var uppercase = "This is an example".ToUpperInvariant(); // yields "THIS IS AN EXAMPLE"   
Title-casing (Pascal or Sentence-casing)

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:
  1. // Example 3: ToTitleCase (upper-cases the first letter of each word)  
  2. var nottitled = CultureInfo.InvariantCulture.TextInfo.ToTitleCase("THIS IS AN EXAMPLE");   
But you’ll notice a minor flaw in the TextInfo.ToTitleCase() method as it does not convert upper-case characters to lower-case (they are considered to be acronyms). This can be a problem that can be easily fixed by simply using the String.ToLower() or String.ToLowerInvariant() methods on your target string:
  1. // Example 3a: ToTitleCase  
  2. var nottitled = CultureInfo.InvariantCulture.TextInfo.ToTitleCase("THIS IS AN EXAMPLE".ToLowerInvariant());   
Or if you wanted to make an extension method for it, you would simply use:


  1. public static class StringExtensions   
  2. {  
  3.    public static string ToTitleCase(this string s)  
  4.    {  
  5.       return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());  
  6.    }  
  7. }  
Which would be used as:
  1. // Example 3a: ToTitleCase (as an extension method)  
  2. var titlecase = "THIS IS AN EXAMPLE".ToTitleCase(); / yields "This Is An Example"   
You will need to include the appropriate namespace to have access to the TextInfoobject, but you just need to add the following using statement to your code:
  1. // Necessary for TextInfo (used in ToTitleCase)  
  2. using System.Globalization;   
Camel-casing (basically Title-casing with the first letter being lower-cased)

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:
  1. public static string ToCamelCase(this string s)   
  2. {  
  3.    // Build the titlecase string  
  4.    var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());  
  5.    // Ensures that there is at-least two characters (so   
  6.    // that the Substring method doesn't freak out)  
  7.    return (titlecase.Length > 1) ? Char.ToLowerInvariant(titlecase[0]) + titlecase.Substring(1) : titlecase;  
  8. }  
Which would be used as:
  1. // Example 4: ToCamelCase (basically ToTitleCase except the first character is lowercased)  
  2. var camelcase = "This is an example".ToCamelCase(); // yields this Is An Example"   
Irish-Casing (capitalizing characters that follow apostrophes such as O’Reilly, etc. )

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:
  1. // Necessary for Irish and Custom-casing  
  2. using System.Text.RegularExpressions;   
After including that, you can easily add in the following code and get started:
  1. public static string ToIrishCase(this string s)   
  2. {  
  3.    // This will build a Titlecased string, but   
  4.    // will uppercase any letter's that appear after  
  5.    // apostrophes (as in names)  
  6.    var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());  
  7.    // Replaces any character after an apostrophe   
  8.    // with it's uppercase variant  
  9.    return Regex.Replace(titlecase, "'(?:.)", m => m.Value.ToUpperInvariant());  
  10. }  
Which would be used again as an extension method:
  1. // Example 5: Special Casing for names  
  2. var irishcase = "O'reilly".ToIrishCase(); // yields "O'Reilly"   
Irish-casing is interesting because it can easily be elaborated on or added to. For instance, if you wanted to add this same logic to not only match characters that occurred after apostrophes but hyphens as well, you would just need to adjust the expression being used from:




The expression will now match both of these characters and capitalize accordingly:
  1. public static string ToExtendedIrishCase(this string s)   
  2. {  
  3.    // This will build a Titlecased string, but will   
  4.    // uppercase any letter's that appear after   
  5.    // apostrophes (as in names)  
  6.    var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());  
  7.    // Replaces any character after an apostrophe or   
  8.    // hyphen with it's uppercase variant  
  9.    return System.Text.RegularExpressions.Regex.Replace(titlecase, @"['-](?:.)", m => m.Value.ToUpperInvariant());  
  10. }  
And an example might look like this:
  1. // Example 5a: ToExtendedIrishCase (like Irishcase,   
  2. // but it also captializes characters after hyphens as well)  
  3. var irishcase = "kathleen hely-hutchinson".ToIrishCase(); // yields "Kathleen Hely-Hutchinson"   
This probably serves an excellent segue to our next and most custom extension method, Custom-casing.

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:
  1. public static string ToCustomCasing(this string s, string[] characters)   
  2. {  
  3.    // If there are no characters to specifically capitalize,  
  4.    // return the initial string  
  5.    if (characters == null || !characters.Any())  
  6.    {  
  7.       return s;  
  8.    }  
  9.    // Replacement expression  
  10.    var replacements = String.Format("[{0}]", String.Join("", characters).ToLowerInvariant());  
  11.    // Replaces any characters that were passed in  
  12.    return System.Text.RegularExpressions.Regex.Replace(s.ToLowerInvariant(), replacements, m => m.Value.ToUpperInvariant());  
  13. }  
Which would look like this when used:
  1. //Example 6: Custom Casing (only upper-cases specific characters)  
  2. var customcase = "This is an example".ToCustomCasing(new string[] { "e""i" }); // yields "thIs Is an ExamplE"  
Overview & Code Examples

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