Truncation and dehumanization
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 source produce Customer…
and CustomerOrderHistory:
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)}");
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…
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 |
DynamicLengthAndPreserveWords | Total length budget, backing up to a word boundary |
DynamicNumberOfCharactersAndPreserveWords | Character 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.
Account for truncation markers and lost information
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.