DEV Community

Anton Martyniuk
Anton Martyniuk

Posted on Originally published at antondevtips.com

Humanizer in .NET: Turn Strings, Dates, and Numbers Into Human-Friendly Text

Software stores data the way machines like it. Users want to read it the way humans like it.

So you write this boilerplate code every day.

You manually split a PascalCase enum into words. You turn 2 into "2nd" with a few if statements.

You format a timestamp as "3 hours ago". You pluralize "item" only when the count is not 1.

It is a few lines here, a helper method there - and it piles up across a codebase.

Humanizer is a small, free .NET library that does all of it for you.

It turns developer-shaped data into human-friendly text with a single method call.

In this post, we will explore:

  • How to Humanize and transform strings
  • How to Humanize enums
  • How to Humanize dates and times
  • How to Turn numbers into words
  • How to Work with culture
  • How to Pluralize and quantify
  • How to Format byte sizes and large numbers
  • Bonus: Roman numerals, collections, and fluent dates

Let's dive in.


πŸ‘‰ Read original article on my newsletter: https://antondevtips.com/blog/humanizer-in-dotnet-turn-strings-dates-and-numbers-into-human-friendly-text

How to Humanize and Transform Strings

First, add Humanizer to your project by installing the following NuGet package:

dotnet add package Humanizer
Enter fullscreen mode Exit fullscreen mode

The core of Humanizer is Humanize(), which turns a developer string into a readable sentence.

It splits strings in PascalCase, camelCase, snake_case, kebab-case, all in lower or UPPER case, into words and applies sentence casing:

"PascalCaseInputStringIsTurnedIntoSentence".Humanize();
// => "Pascal case input string is turned into sentence"

"Underscored_input_string_is_turned_into_sentence".Humanize();
// => "Underscored input string is turned into sentence"
Enter fullscreen mode Exit fullscreen mode

Transform changes the casing without splitting words. It takes one or more strategies:

"Sentence casing".Transform(To.TitleCase);    // => "Sentence Casing"
"Sentence casing".Transform(To.LowerCase);    // => "sentence casing"
"Sentence casing".Transform(To.SentenceCase); // => "Sentence casing"
Enter fullscreen mode Exit fullscreen mode

Dehumanize is the reverse of Humanize - it turns a sentence back into a PascalCase identifier:

"Pascal case input string is turned into sentence".Dehumanize();
// => "PascalCaseInputStringIsTurnedIntoSentence"
Enter fullscreen mode Exit fullscreen mode

And Truncate shortens text to a length, adding an ellipsis:

"Long text to truncate".Truncate(10);
// => "Long text…"
Enter fullscreen mode Exit fullscreen mode

These four cover most of the string formatting you write by hand: generating labels, display names, and identifiers from code.

How to Humanize Enums

Enum names are in PascalCase, and your UI needs readable labels.

Normally, you map each enum member to a label by hand:

var label = status switch
{
    ShipmentStatus.OutForDelivery => "Out for delivery",
    ShipmentStatus.InTransit => "In transit",
    // ... a line for every member
};
Enter fullscreen mode Exit fullscreen mode

Calling Humanize() on the enum value does the same thing for free. It splits the name into words, so the switch disappears:

public enum ShipmentStatus
{
    Pending,
    InTransit,
    OutForDelivery,
    Delivered
}

ShipmentStatus.OutForDelivery.Humanize();  // => "Out for delivery"
ShipmentStatus.InTransit.Humanize();       // => "In transit"
Enter fullscreen mode Exit fullscreen mode

When you need a label that does not match the member name, add a [Description] attribute and Humanizer uses it:

public enum ShipmentStatus
{
    [Description("Awaiting pickup")]
    Pending,
    // ...
}

ShipmentStatus.Pending.Humanize();  // => "Awaiting pickup"
Enter fullscreen mode Exit fullscreen mode

You can also go the other way, turning a display string back into the enum value with DehumanizeTo:

"Out for delivery".DehumanizeTo<ShipmentStatus>();
// => ShipmentStatus.OutForDelivery
Enter fullscreen mode Exit fullscreen mode

This alone removes a surprising amount of mapping code from the boundary between your domain and your UI.


πŸ‘‰ Read original article on my newsletter: https://antondevtips.com/blog/humanizer-in-dotnet-turn-strings-dates-and-numbers-into-human-friendly-text

Top comments (0)