Skip to main content
Version: 4.0 (next)

Transform strings, identifiers, and casing

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 source covers readable labels, custom acronym casing, ampersands, snake/kebab conversion, dehumanization, and truncation:

Program.cs
using System.Globalization;
using Humanizer;

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");

Vocabularies.Default.AddAcronym("iOS");

Console.WriteLine($"Label: {"CustomerOrderHistory".Humanize()}");
Console.WriteLine($"Acronym: {"IOSSettings".Humanize()}");
Console.WriteLine($"Ampersand: {"Research&Development".Humanize()}");
Console.WriteLine($"Snake case: {"CustomerOrderHistory".Underscore()}");
Console.WriteLine($"Preserved case: {"HTMLParser".Underscore(preserveCase: true)}");
Console.WriteLine($"Kebab case: {"CustomerOrderHistory".Kebaberize()}");
Console.WriteLine($"Identifier: {"customer order history".Dehumanize()}");
Console.WriteLine($"Truncated: {"Customer order history".Truncate(9)}");
Output
Label: Customer order history
Acronym: iOS settings
Ampersand: Research & development
Snake case: customer_order_history
Preserved case: HTML_Parser
Kebab case: customer-order-history
Identifier: CustomerOrderHistory
Truncated: Customer…

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.

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

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

DestinationAPI
PascalCasePascalize
camelCaseCamelize
snake_caseUnderscore
kebab-caseKebaberize
Replace underscores with hyphensDasherize or Hyphenate
Title-like identifier wordsTitleize

Dehumanize is the direct readable-text-to-PascalCase operation. Use the named inflector transformations when the destination convention matters.

Underscore() lowercases its output. 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.

Treat identifier transformations as lossy

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.