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:
using System.Globalization;
using Humanizer;
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
AssertEqual("Customer order history", "CustomerOrderHistory".Humanize());
AssertEqual("Customer Order History", "CustomerOrderHistory".Humanize(LetterCasing.Title));
AssertEqual("CustomerOrderHistory", "customer order history".Dehumanize());
AssertEqual("customer_order_history", "CustomerOrderHistory".Underscore());
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");
static void AssertEqual(string expected, string? actual)
{
if (actual != expected)
throw new InvalidOperationException($"Expected '{expected}', got '{actual}'.");
}
Choose the truncator
| Strategy | Meaning of length |
|---|---|
FixedLength | Total returned length, including the marker |
FixedNumberOfCharacters | Characters retained before adding the marker |
FixedNumberOfWords | Whole words retained |
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 three selected-release 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. 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. Humanizer 2.11.10 exposes the three fixed truncator strategies listed above. The two built-in preserve-word strategies begin in 3.0.1; implement ITruncator when this selected release needs that behavior.