Introduction
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.
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
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 :
var lowercase = "This is an example".ToLowerInvariant();
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.
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)
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 :
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 :
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 :
public static class StringExtensions
{
public static string ToTitleCase(this string s)
{
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());
}
}
which would be used as :
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 :
using System.Globalization;
Camel-casing (basically Title-casing with the first letter being lower-cased)
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 :
public static string ToCamelCase(this string s)
{
var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());
return (titlecase.Length > 1) ? Char.ToLowerInvariant(titlecase[0]) + titlecase.Substring(1) : titlecase;
}
which would be used as :
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)
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 :
using System.Text.RegularExpressions;
After including that, you can easily add in the following code and get started :
public static string ToIrishCase(this string s)
{
var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());
return Regex.Replace(titlecase, "'(?:.)", m => m.Value.ToUpperInvariant());
}
which would be used again as an extension method :
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 :
public static string ToExtendedIrishCase(this string s)
{
var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLowerInvariant());
return System.Text.RegularExpressions.Regex.Replace(titlecase,
@"['\-](?:.)", m => m.Value.ToUpperInvariant());
}
and an example might look like this :
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.
Custom-casing would be an 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 (characters == null || !characters.Any())
{
return s;
}
var replacements = String.Format("[{0}]",
String.Join("", characters).ToLowerInvariant());
return System.Text.RegularExpressions.Regex.Replace(s.ToLowerInvariant(),
replacements, m => m.Value.ToUpperInvariant());
}
which would look like this when used :
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 :