String Humanization
October 21, 2025 · View on GitHub
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.
Overview
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
Basic Usage
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"
Acronym Handling
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"
Letter Casing
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
How It Works
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
Real-World Examples
Display Test Method Names
// In a testing framework
public void ItShouldCalculateTheTotalPrice()
{
// Test implementation
}
// Display to user:
nameof(ItShouldCalculateTheTotalPrice).Humanize()
// => "It should calculate the total price"
Display Property Names in UI
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"
Convert API Field Names
// 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
Version 3.0 Behavioral Change
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() // => "Майк"
Related Topics
- String Dehumanization - Convert back to PascalCase
- String Transformations - Custom transformations
- Inflector Methods - Titleize, Pascalize, Camelize, etc.