Skip to main content
Version: 4.0 (next)

Truncation and dehumanization

Orientation

Use Truncate to fit display text into a character or word budget without hand-written substring rules. Select a built-in ITruncator according to what the length means. Use string Dehumanize separately when readable words must become a PascalCase-shaped identifier.

Example

The truncation and dehumanization calls in this verified source produce Customer…, preserve four complete words, and produce CustomerOrderHistory:

Program.cs
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}'.");
}

Choose the truncator

StrategyMeaning of length
FixedLengthTotal returned length, including the marker
FixedNumberOfCharactersCharacters retained before adding the marker
FixedNumberOfWordsWhole words retained
DynamicLengthAndPreserveWordsTotal length budget, backing up to a word boundary
DynamicNumberOfCharactersAndPreserveWordsCharacter budget, backing up to a word boundary

The default marker is the single-character ellipsis . Pass a custom marker and TruncateFrom.Left when preserving the end of a value matters. Implement ITruncator only when none of the five contracts matches the application’s display rule.

Dehumanize readable text

Dehumanize removes word separators and produces PascalCase. It is suitable for application-controlled labels and scaffolding, not for reconstructing an original identifier.

Pitfall

The marker consumes budget for fixed-length strategies. When the marker itself fills the available length, preserve-word strategies can return only the marker or an empty result. Truncation preserves null input, but an application still needs to decide whether null and empty text mean the same thing.

Dehumanize is not a lossless inverse: punctuation, casing, acronym, and word-boundary information can be discarded.

Version notes

The verified source compiles and runs against the selected package. Truncator strategies exist across the documented corpus, but overload order and Unicode behavior vary; consult the selected-version API before editing an old call.