There is a possibility to convert any string to title case, taking care of culture in .NET. However this functionality is quite hidden and has an exception rule for words which are written entirely in uppercase. So I decided to create some extension methods on type string for easier access an with the possibility to bypass the "only-uppercase" exception rule.
using System;
using System.Globalization;
namespace Luegisdorf
{
/// <summary>
/// Contains extensions for type string
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Converts the specified string to title case.
/// </summary>
/// <param name="subject">
/// The specified string converted to title case.
/// </param>
/// <param name="keepOnlyUppercase">
/// If <c>true</c>, only-uppercase <c>subject</c>
/// will be retained as uppercase, otherwise only-uppercase
/// <c>subject</c> will title cased as well.
/// </param>
/// <param name="cultureInfo">
/// The culture information used for conversion.
/// </param>
/// <returns>
/// The title cased <c>subject</c>
/// </returns>
public static string ToTitleCase(this string subject, bool keepOnlyUppercase,
CultureInfo cultureInfo)
{
var textToConvert = subject
?? throw new ArgumentNullException(nameof(subject));
var cultureInformation = cultureInfo
?? throw new ArgumentNullException(nameof(cultureInfo));
if (!keepOnlyUppercase)
{
/* since TextInfo.ToTitleCase keeps only uppercase words,
* we need to ensure the subject does not contain entirely
* uppercase words
*/
textToConvert = cultureInformation.TextInfo.ToLower(textToConvert);
}
return cultureInfo.TextInfo.ToTitleCase(textToConvert);
}
/// <summary>
/// Converts the specified string to title case.
/// </summary>
/// <param name="subject">
/// The specified string converted to title case.
/// </param>
/// <param name="keepOnlyUppercase">
/// If <c>true</c>, only-uppercase <c>subject</c>
/// will be retained as uppercase, otherwise only-uppercase
/// <c>subject</c> will title cased as well.
/// </param>
/// <returns>
/// The title cased <c>subject</c>
/// </returns>
public static string ToTitleCase(this string subject, bool keepOnlyUppercase)
=> ToTitleCase(subject, keepOnlyUppercase, CultureInfo.CurrentCulture);
/// <summary>
/// Converts the specified string to title case
/// (including for words that are entirely in uppercase).
/// </summary>
/// <param name="subject">
/// The specified string converted to title case.
/// </param>
/// <param name="cultureInfo">
/// The culture information used for conversion.
/// </param>
/// <returns>
/// The title cased <c>subject</c>
/// </returns>
public static string ToTitleCase(this string subject, CultureInfo cultureInfo)
=> ToTitleCase(subject, false, cultureInfo);
/// <summary>
/// Converts the specified string to title case
/// (including for words that are entirely in uppercase).
/// </summary>
/// <param name="subject">
/// The specified string converted to title case.
/// </param>
/// <returns>
/// The title cased <c>subject</c>
/// </returns>
public static string ToTitleCase(this string subject)
=> ToTitleCase(subject, CultureInfo.CurrentCulture);
}
}