Last active
December 30, 2015 07:09
-
-
Save davidalpert/7793690 to your computer and use it in GitHub Desktop.
StringExtensions.ToWords() converts "EnteringAirTemperature" to "Entering Air Temperature"; useful as a default convention for converting type names to human readable labels for display purposes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class StringExtensions | |
{ | |
/// <summary> | |
/// Converts the string representation of <paramref name="src"/> to a human-readable format. | |
/// </summary> | |
/// <example>"SomeEnumValue" becomes "Some Enum Value"</example> | |
/// <example>"Some_Class_Name" becomes "Some Class Name"</example> | |
/// <param name="src">the raw input to humanize</param> | |
/// <param name="casingStyle"> | |
/// An optional <see cref="CasingStyle"/> lets you control how the string is rewritten. | |
/// </param> | |
/// <returns>A human-readible representation of <paramref name="src"/></returns> | |
public static string ToWords(this object src, CasingStyle casingStyle = CasingStyle.AsIs) | |
{ | |
return (src ?? String.Empty).ToString().ToWords(casingStyle); | |
} | |
public static string ToWords(this string src) | |
{ | |
return ToWords(src, CasingStyle.AsIs); | |
} | |
static readonly Dictionary<CasingStyle, Func<char, char, bool, char>> CharTransformsByCasingStyle | |
= new Dictionary<CasingStyle, Func<char, char, bool, char>> { | |
{CasingStyle.AsIs, (c, prev, isFirstChar) => c}, | |
{CasingStyle.TitleCase, (c, prev, isFirstChar) => c}, | |
{CasingStyle.AllLower, (c, prev, isFirstChar) => c.ToLower()}, | |
{CasingStyle.SentanceCase, (c, prev, isFirstChar) => isFirstChar ? c.ToUpper() : c.ToLower()} | |
}; | |
public static string ToWords(this string src, CasingStyle casingStyle = CasingStyle.AsIs) | |
{ | |
var sb = new StringBuilder(); | |
var transform = CharTransformsByCasingStyle[casingStyle]; | |
char last = char.MinValue; | |
bool isFirst = true; | |
foreach (char c in src) | |
{ | |
if (c == '_') | |
{ | |
sb.Append(' '); | |
last = ' '; | |
} | |
else | |
{ | |
if (char.IsLower(last) && (char.IsUpper(c))) | |
{ | |
sb.Append(' '); | |
last = ' '; | |
} | |
sb.Append(transform(c, last, isFirst)); | |
last = c; | |
} | |
isFirst = false; | |
} | |
string value = sb.ToString().Trim(); | |
return casingStyle == CasingStyle.AllLower ? value.ToLowerInvariant() : value; | |
} | |
static char ToLower(this char c) | |
{ | |
return c.ToString(CultureInfo.InstalledUICulture).ToLower().ToCharArray()[0]; | |
} | |
static char ToUpper(this char c) | |
{ | |
return c.ToString(CultureInfo.InvariantCulture).ToUpper().ToCharArray()[0]; | |
} | |
public static string Underscore(this string src, bool forceLowerCase = true) | |
{ | |
return src.ToWords(forceLowerCase ? CasingStyle.AllLower : CasingStyle.AsIs).Replace(' ', '_'); | |
} | |
} | |
/// <summary> | |
/// Casing styles for the <see cref="StringExtensions.Humanize"/> methods | |
/// </summary> | |
public enum CasingStyle | |
{ | |
AsIs, | |
TitleCase, // Each word capitalized | |
SentanceCase, // First word capitalized, following words lowercase | |
AllLower, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment