docs/string-humanization.md
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:
PascalCaseString → Pascal case stringcamelCaseString → Camel case stringunderscored_string → Underscored stringdash-separated-string → Dash separated stringusing 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 lowercaseThe humanization process applies several rules in order:
// 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 date
In 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() // => "Майк"