Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Handling Casing Issues though Extension Methods in C# and Visual Basic

0.00/5 (No votes)
18 Apr 2014CPOL5 min read 8.5K  
How to handle casing issues through 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.

This post covers a variety of methods to handling different casing formats for strings within .NET.

This post covers a variety of methods to handle different casing formats for strings within .NET.

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

An example of lower-casing.

An example of 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:

C#
//Example 1: ToLowerCase (all lower-case)
var lowercase = "This is an example".ToLowerInvariant();

Upper-casing

An example of Upper-casing

An example of Upper-casing

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.

C#
//Example 2: ToUpperCase (all upper-case)
var uppercase = "This is an example".ToUpperInvariant();

Title-casing (Commonly Known as Pascal-casing or Sentence-casing)

An example of Title-casing (also known as Pascal-casing or Sentence-casing)

An example of Title-casing (also known as Pascal-casing or Sentence-casing)

Title-casing is a 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 in some instances involving proper names, titles and can be a bit trickier to implement than it’s upper and lower-case brethren.

Commonly, you would use the TextInfo.ToTitleCase() method to handle this as follows:

C#
//Example 3: ToTitleCase (upper-cases the first letter of each word)
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:

C#
//Example 3a: ToTitleCase
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:

C#
public static class StringExtensions
{
       public static string ToTitleCase(this string s)
       {
               return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());
       }
}

which would be used as:

C#
//Example 3a: ToTitleCase (as an extension method)
var titlecase = "THIS IS AN EXAMPLE".ToTitleCase();

One note – you will need to include the appropriate namespace to have access to the TextInfo object, but you just need to add the following using statement to your code:

C#
//Necessary for TextInfo (used in ToTitleCase)
using System.Globalization;

Camel-casing (Basically Title-casing with the First Letter Being lower-cased)

An Example of Camel-casing

An Example of Camel-casing

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:

C#
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;
}

which would be used as:

C#
//Example 4: ToCamelCase (basically ToTitleCase except the first character is lowercased)
var camelcase = "This is an example".ToCamelCase();

Irish-Casing (Capitalizing Characters that Follow Apostrophes such as O’Reilly, etc.)

An example of Irish-casing (letters following an apostrophe are capitalized)

An example of Irish-casing (letters following an apostrophe are capitalized)

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:

C#
//Necessary for Irish and Custom-casing
using System.Text.RegularExpressions;

After including that, you can easily add in the following code and get started:

C#
public static string ToIrishCase(this string s)
{
       //This will build a Titlecased string, but will uppercase any letters 
       //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());
}

which would be used again as an extension method:

C#
//Example 5: Special Casing for names
var irishcase = "O'reilly".ToIrishCase();

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:

'(?:.)

to:

['\-](?:.)

The expression will now match both of these characters and capitalize accordingly:

C#
public static string ToExtendedIrishCase(this string s)
{
       //This will build a Titlecased string, but will uppercase any letters 
       //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());
}

and an example might look like this:

C#
//Example 5a: ToExtendedIrishCase (like Irishcase, 
//but it also captializes characters after hyphens as well)
var irishcase = "kathleen hely-hutchinson".ToIrishCase();

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)

An example of Custom-casing (this allows you to explicitly define the characters you want to upper-case). In this example, vowels are upper-cased.

An example of Custom-casing (this allows you to explicitly define the characters you want to upper-case). In this example, vowels are 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:

C#
public static string ToCustomCasing(this string s, string[] characters)
{
       //If there are no characters to specifically capialize - 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());
}

which would look like this when used:

C#
//Example 6: Custom Casing (only upper-cases specific characters)
var customcase = "This is an example".ToCustomCasing(new string[] { "e", "i" });

Overview

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:

Filed under: CodeProject, Development Image 8 Image 9 Image 10 Image 11 Image 12 Image 13 Image 14 Image 15

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)