Transform strings, identifiers, and casing
Orientation
Use Humanize to turn program-shaped identifiers into display text. Use the identifier transformations when code, file, or route naming is the destination. Apply LetterCasing or a cultured transformer only after deciding the word boundaries; casing and identifier conversion solve different problems.
Example
This verified source covers readable labels, custom acronym casing, ampersands, explicit casing, Pascal/snake/kebab conversion, dehumanization, truncation, and a cultured transformer:
using System.Globalization;
using Humanizer;
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
Vocabularies.Default.AddAcronym("iOS");
AssertEqual("Customer order history", "CustomerOrderHistory".Humanize());
AssertEqual("Customer Order History", "CustomerOrderHistory".Humanize(LetterCasing.Title));
AssertEqual("iOS settings", "IOSSettings".Humanize());
AssertEqual("Research & development", "Research&Development".Humanize());
AssertEqual("CustomerOrderHistory", "customer order history".Dehumanize());
AssertEqual("customer_order_history", "CustomerOrderHistory".Underscore());
AssertEqual("HTML_Parser", "HTMLParser".Underscore(preserveCase: true));
AssertEqual("customer-order-history", "CustomerOrderHistory".Kebaberize());
AssertEqual("CustomerOrderHistory", "customer_order_history".Pascalize());
AssertEqual("Customer…", "Customer order history".Truncate(9));
AssertEqual(
"Text with more words…",
"Text with more words than the limit".Truncate(4, Truncator.FixedNumberOfWords));
AssertEqual("Order History", "order history".Transform(To.TitleCase));
Console.WriteLine("Customer order history; iOS settings; Research & development");
static void AssertEqual(string expected, string? actual)
{
if (actual != expected)
throw new InvalidOperationException($"Expected '{expected}', got '{actual}'.");
}
Turn an identifier into a label
Humanize recognizes PascalCase, camelCase, underscores, dashes, numbers, and common acronym boundaries. Pass LetterCasing.Sentence, Title, LowerCase, or AllCaps when the label needs a known presentation case. All-capital input is treated as an acronym and can remain unchanged until a casing operation is requested.
On Humanizer 4, Humanize preserves ampersands as separate tokens instead
of discarding them with other punctuation. For example,
"Research&Development".Humanize() returns "Research & development".
Other punctuation such as + and / is still removed.
Preserve domain acronym casing
On Humanizer 4, call Vocabularies.Default.AddAcronym once during startup
when an acronym has required output casing. Registration matches
case-insensitively, while the registered spelling supplies the output:
registering iOS makes "IOSSettings".Humanize() return "iOS settings".
An acronym must be non-empty and contain only letters.
Acronym registration is process-wide. Treat it like the other default vocabulary configuration: register it before concurrent work begins and keep tests that change it isolated.
Convert between identifier conventions
| Destination | API |
|---|---|
| PascalCase | Pascalize |
| camelCase | Camelize |
| snake_case | Underscore |
| kebab-case | Kebaberize |
| Replace underscores with hyphens | Dasherize or Hyphenate |
| Title-like identifier words | Titleize |
Dehumanize is the direct readable-text-to-PascalCase operation. Use the named inflector transformations when the destination convention matters.
Underscore() lowercases its output. On Humanizer 4, pass
preserveCase: true to retain the input casing while inserting boundaries:
"HTMLParser".Underscore(preserveCase: true) returns "HTML_Parser".
Compose casing transformations
Transform(To.TitleCase) and its sentence, upper, and lower siblings use cultured string transformers. Pass a CultureInfo to the cultured overload. Implement IStringTransformer or ICulturedStringTransformer for a local reusable rule without changing process-global configuration.
Pitfall
Identifier conversions are intentionally lossy. Acronym boundaries, punctuation, whitespace, and Unicode can prevent a round trip. Dasherize and Hyphenate primarily replace underscores; use Kebaberize when the input needs complete word-boundary normalization.
Title and sentence casing use culture-sensitive text operations. Scope culture when output must be deterministic, especially for Turkish and Azerbaijani casing, and do not assume title casing implements a particular editorial style guide.
Version notes
The verified executable compiles and runs against the selected package. Core
transformations also exist in 2.x, but custom acronym registration,
ampersand preservation, and the case-preserving Underscore overload are
Humanizer 4 behavior. Humanizer 3 also changes several delimiter and
no-letter edge cases. Use the selected snapshot when exact legacy behavior
matters.