String humanization transforms computerized strings (like class names, method names, or property names) into human-readable text. This is particularly useful when displaying programming identifiers to end users.
The Humanize extension method intelligently handles:
- PascalCase:
PascalCaseString→Pascal case string - camelCase:
camelCaseString→Camel case string - Underscores:
underscored_string→Underscored string - Dashes:
dash-separated-string→Dash separated string
using Humanizer;
"PascalCaseInputStringIsTurnedIntoSentence".Humanize()
// => "Pascal case input string is turned into sentence"
"Underscored_input_string_is_turned_into_sentence".Humanize()
// => "Underscored input string is turned into sentence"
"dash-separated-string".Humanize()
// => "Dash separated string"Strings containing only uppercase letters are treated as acronyms and left unchanged:
"HTML".Humanize() // => "HTML"
"HUMANIZER".Humanize() // => "HUMANIZER"To force humanization of all-caps strings, use the Transform method:
"HUMANIZER".Transform(To.LowerCase, To.TitleCase) // => "Humanizer"Control the output casing:
"CanReturnTitleCase".Humanize(LetterCasing.Title)
// => "Can Return Title Case"
"CanReturnLowerCase".Humanize(LetterCasing.LowerCase)
// => "can return lower case"
"CanHumanizeIntoUpperCase".Humanize(LetterCasing.AllCaps)
// => "CAN HUMANIZE INTO UPPER CASE"
"some string".Humanize(LetterCasing.Sentence)
// => "Some string"Available casing options:
LetterCasing.Title- Capitalizes the first letter of each wordLetterCasing.Sentence- Capitalizes only the first letterLetterCasing.AllCaps- All uppercaseLetterCasing.LowerCase- All lowercase
The humanization process applies several rules in order:
- If the entire input is uppercase (an acronym), it returns unchanged
- Handles freestanding underscores/dashes (e.g., "some _ string")
- Splits on underscores and dashes
- Breaks up PascalCase and camelCase text
- Capitalizes the first letter of the result
// In a testing framework
public void ItShouldCalculateTheTotalPrice()
{
// Test implementation
}
// Display to user:
nameof(ItShouldCalculateTheTotalPrice).Humanize()
// => "It should calculate the total price"public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
// Generate form labels automatically
nameof(User.FirstName).Humanize() // => "First name"
nameof(User.DateOfBirth).Humanize() // => "Date of birth"// API returns: "account_status", "last_login_date"
var fields = new[] { "account_status", "last_login_date" };
foreach (var field in fields)
{
Console.WriteLine(field.Humanize());
}
// Output:
// Account status
// Last login dateIn version 3.0, Humanize and Titleize now preserve input strings that contain no recognized letters (e.g., special characters, unrecognized Unicode scripts) instead of returning an empty string:
// Before v3.0: returned ""
// v3.0 and later: returns "@@"
"@@".Humanize() // => "@@"
// Cyrillic and other Unicode scripts are preserved
"Майк".Titleize() // => "Майк"- String Dehumanization - Convert back to PascalCase
- String Transformations - Custom transformations
- Inflector Methods - Titleize, Pascalize, Camelize, etc.