Humanizerは、C#における入力文字列を文に変換するための多数のツールと拡張メソッドを提供します。
Humanizerは、特にユーザーフレンドリーな形式で情報を表示する際に、データを扱うプロセスを簡素化し、人間らしくする強力で柔軟な.NETライブラリです。 日付を相対時間("3日前")に変換、複数形にし、数字を単語としてフォーマットするなど、C#.NETでタスクをエレガントに処理するためのツールと拡張メソッドを提供します。
この記事では、C#でのHumanizerの詳細なチュートリアルを説明します。 また、C# PDFライブラリのためにHumanizerとIronPDFを使用してPDFドキュメントを生成する方法についても説明します。
C#でのHumanizerのセットアップ
Humanizerを始めるには、NuGet経由でライブラリをインストールする必要があります。 プロジェクトでは、パッケージマネージャコンソールを通じて次のコマンドでこれを行うことができます。
Install-Package Humanizer
代わりに、.NET Core CLIを使用している場合は、次のコマンドでHumanizerを追加できます。
dotnet add package Humanizer
インストール後、適切な名前空間をC#ファイルに含めることでHumanizerを使用し始めることができます。
using Humanizer;using Humanizer;日付と時間の人間化
Humanizerの最も一般的な使用方法の1つは、Humanizeメソッドを使用して、日時を人間が読みやすい形式、タイムスパン、数値、量に変換することです。 これは、"2時間前"や"5日後"のような相対時間を表示する際に特に便利です。
例: 相対時間の人間化
using System;
class HumanizerDemo
{
static void Main()
{
DateTime pastDate = DateTime.Now.AddDays(-3);
// Humanize the past date, which converts it to a relative time format
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
// Humanize the future date, presenting it in relative time
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"
Console.WriteLine("Humanized Past Date: " + humanizedTime);
Console.WriteLine("Humanized Future Date: " + futureHumanizedTime);
}
}using System;
class HumanizerDemo
{
static void Main()
{
DateTime pastDate = DateTime.Now.AddDays(-3);
// Humanize the past date, which converts it to a relative time format
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
// Humanize the future date, presenting it in relative time
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"
Console.WriteLine("Humanized Past Date: " + humanizedTime);
Console.WriteLine("Humanized Future Date: " + futureHumanizedTime);
}
}Humanizerの拡張メソッドは、さまざまな時間単位を自動的に処理し、文法的な正確さも調整します。

TimeSpansの人間化
Humanizerは、TimeSpanオブジェクトも人間化し、持続時間を読みやすい形式で表示するのを簡単にします。
例: TimeSpanの人間化
using System;
class TimeSpanHumanizerDemo
{
static void Main()
{
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
// Humanizing the TimeSpan into hours and minutes
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"
Console.WriteLine("Humanized TimeSpan: " + humanizedTimeSpan);
}
}using System;
class TimeSpanHumanizerDemo
{
static void Main()
{
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
// Humanizing the TimeSpan into hours and minutes
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"
Console.WriteLine("Humanized TimeSpan: " + humanizedTimeSpan);
}
}
数値の処理
Humanizerは、数値を人間が読みやすい単語に変換し、序数を処理するためのいくつかのメソッドを提供します。
例: 数字を単語に変換する
using System;
class NumberHumanizerDemo
{
static void Main()
{
int number = 123;
// Convert number to words
string words = number.ToWords(); // Output: "one hundred and twenty-three"
Console.WriteLine("Number in Words: " + words);
}
}using System;
class NumberHumanizerDemo
{
static void Main()
{
int number = 123;
// Convert number to words
string words = number.ToWords(); // Output: "one hundred and twenty-three"
Console.WriteLine("Number in Words: " + words);
}
}
例: 数字を序数に変換する
using System;
class OrdinalHumanizerDemo
{
static void Main()
{
int number = 21;
// Convert number to ordinal words
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"
Console.WriteLine("Ordinal Number: " + ordinal);
}
}using System;
class OrdinalHumanizerDemo
{
static void Main()
{
int number = 21;
// Convert number to ordinal words
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"
Console.WriteLine("Ordinal Number: " + ordinal);
}
}
複数形と単数形の変換
Humanizerは、単語を単数形と複数形の間で簡単に変換することができ、量に基づいて動的に長文を生成するのに便利です。
例: 単語の複数化と単数化
using System;
class PluralizationDemo
{
static void Main()
{
string singular = "car";
// Pluralize the word
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
// Singularize the word
string singularForm = word.Singularize(); // Output: "person"
Console.WriteLine("Plural of 'car': " + plural);
Console.WriteLine("Singular of 'people': " + singularForm);
}
}using System;
class PluralizationDemo
{
static void Main()
{
string singular = "car";
// Pluralize the word
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
// Singularize the word
string singularForm = word.Singularize(); // Output: "person"
Console.WriteLine("Plural of 'car': " + plural);
Console.WriteLine("Singular of 'people': " + singularForm);
}
}Humanizerは、不規則な複数形と単数形も扱い、多様なユースケースに対して強力です。

Enumsのフォーマット
EnumsはC#アプリケーションで名前付き定数のセットを表すために頻繁に使用されます。 Humanizerは、enum値を人間が読みやすい文字列に変換できます。
例: Enumsの人間化
using System;
public enum MyEnum
{
FirstValue,
SecondValue
}
class EnumHumanizerDemo
{
static void Main()
{
MyEnum enumValue = MyEnum.FirstValue;
// Humanizing enum to a readable format
string humanizedEnum = enumValue.Humanize(); // Output: "First value"
Console.WriteLine("Humanized Enum: " + humanizedEnum);
}
}using System;
public enum MyEnum
{
FirstValue,
SecondValue
}
class EnumHumanizerDemo
{
static void Main()
{
MyEnum enumValue = MyEnum.FirstValue;
// Humanizing enum to a readable format
string humanizedEnum = enumValue.Humanize(); // Output: "First value"
Console.WriteLine("Humanized Enum: " + humanizedEnum);
}
}このメソッドは、UIでユーザーフレンドリーなラベルを表示する際に特に便利です。

バイトサイズの人間化
Humanizerのもう一つの便利な機能はバイトサイズを人間化し、大きなバイト値をKB、MB、GBなどの読みやすい形式に変換することです。
例: バイトサイズの人間化
using System;
class ByteSizeHumanizerDemo
{
static void Main()
{
long bytes = 1048576;
// Humanize bytes to a readable size format
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"
Console.WriteLine("Humanized Byte Size: " + humanizedBytes);
}
}using System;
class ByteSizeHumanizerDemo
{
static void Main()
{
long bytes = 1048576;
// Humanize bytes to a readable size format
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"
Console.WriteLine("Humanized Byte Size: " + humanizedBytes);
}
}
高度なシナリオ
Humanizerは、上記の基本的なシナリオに限られません。 それは、Truncateメソッドと複数の言語と拡張機能をサポートしています。
例: DateTimeオフセットの人間化
Humanizerは、タイムゾーンを扱うアプリケーションに役立つDateTimeOffsetも処理できます。
using System;
class DateTimeOffsetHumanizerDemo
{
static void Main()
{
DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
// Humanize DateTimeOffset
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"
Console.WriteLine("Humanized DateTimeOffset: " + humanizedDateTimeOffset);
}
}using System;
class DateTimeOffsetHumanizerDemo
{
static void Main()
{
DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
// Humanize DateTimeOffset
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"
Console.WriteLine("Humanized DateTimeOffset: " + humanizedDateTimeOffset);
}
}
パフォーマンスの考慮事項
Humanizerは効率的に設計されていますが、他のライブラリと同様に、そのパフォーマンスは使用方法に依存します。 特に大規模なデータセットやリアルタイム処理を扱う高性能を必要とするアプリケーションの場合、頻繁な人間化操作の影響を考慮することが重要です。
C#用IronPDF
IronPDFは、.NETアプリケーション向けの包括的なPDF生成および操作ライブラリです。 開発者がPDFファイルを簡単に作成、読み取り、編集、およびコンテンツを抽出することを可能にします。 IronPDFは、ユーザーフレンドリーな設計で、HTMLからPDFへの変換、ドキュメントのマージ、透かしの追加など、多くの機能を提供します。 その多機能性と強力な機能は、C#プロジェクトでのPDFドキュメントの処理に最適です。
NuGetパッケージマネージャを使用したIronPDFのインストール
NuGetパッケージマネージャを使用してIronPDFをインストールする手順は次の通りです。
Visual Studio でプロジェクトを開きます。
- Visual Studioを起動し、既存のC#プロジェクトを開くか、新しいプロジェクトを作成します。
NuGet パッケージ マネージャーを開きます。
- ソリューションエクスプローラでプロジェクトを右クリックします。
- コンテキストメニューから"Manage NuGet Packages…"を選択します。

IronPDFをインストールします。
- NuGetパッケージマネージャで"参照"タブに移動します。
- IronPDFを検索します。
- 検索結果からIronPDFパッケージを選択します。
- "インストール"ボタンをクリックして、プロジェクトにIronPDFを追加します。

これらの手順に従うことで、IronPDFがインストールされ、プロジェクトでのPDF操作の強力な機能を活用できるようになります。
C# HumanizerおよびIronPDFコード例
using Humanizer;
using IronPdf;
using System;
using System.Collections.Generic;
class PDFGenerationDemo
{
static void Main()
{
// Instantiate the PDF renderer
var renderer = new ChromePdfRenderer();
// Generate humanized content
List<string> content = GenerateHumanizedContent();
// HTML content template for the PDF
string htmlContent = "<h1>Humanizer Examples</h1><ul>";
// Build the list items to add to the HTML content
foreach (var item in content)
{
htmlContent += $"<li>{item}</li>";
}
htmlContent += "</ul>";
// Render the HTML into a PDF document
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to a file
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF document generated successfully: output.pdf");
}
/// <summary>
/// Generates a list of humanized content examples
/// </summary>
/// <returns>List of humanized content as strings</returns>
static List<string> GenerateHumanizedContent()
{
List<string> content = new List<string>();
// DateTime examples
DateTime pastDate = DateTime.Now.AddDays(-3);
DateTime futureDate = DateTime.Now.AddHours(5);
content.Add($"DateTime.Now: {DateTime.Now}");
content.Add($"3 days ago: {pastDate.Humanize()}");
content.Add($"In 5 hours: {futureDate.Humanize()}");
// TimeSpan examples
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
content.Add($"TimeSpan of 123 minutes: {timeSpan.Humanize()}");
// Number examples
int number = 12345;
content.Add($"Number 12345 in words: {number.ToWords()}");
content.Add($"Ordinal of 21: {21.ToOrdinalWords()}");
// Pluralization examples
string singular = "car";
content.Add($"Plural of 'car': {singular.Pluralize()}");
string plural = "children";
content.Add($"Singular of 'children': {plural.Singularize()}");
// Byte size examples
long bytes = 1048576;
content.Add($"1,048,576 bytes: {bytes.Bytes().Humanize()}");
return content;
}
}using Humanizer;
using IronPdf;
using System;
using System.Collections.Generic;
class PDFGenerationDemo
{
static void Main()
{
// Instantiate the PDF renderer
var renderer = new ChromePdfRenderer();
// Generate humanized content
List<string> content = GenerateHumanizedContent();
// HTML content template for the PDF
string htmlContent = "<h1>Humanizer Examples</h1><ul>";
// Build the list items to add to the HTML content
foreach (var item in content)
{
htmlContent += $"<li>{item}</li>";
}
htmlContent += "</ul>";
// Render the HTML into a PDF document
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to a file
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF document generated successfully: output.pdf");
}
/// <summary>
/// Generates a list of humanized content examples
/// </summary>
/// <returns>List of humanized content as strings</returns>
static List<string> GenerateHumanizedContent()
{
List<string> content = new List<string>();
// DateTime examples
DateTime pastDate = DateTime.Now.AddDays(-3);
DateTime futureDate = DateTime.Now.AddHours(5);
content.Add($"DateTime.Now: {DateTime.Now}");
content.Add($"3 days ago: {pastDate.Humanize()}");
content.Add($"In 5 hours: {futureDate.Humanize()}");
// TimeSpan examples
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
content.Add($"TimeSpan of 123 minutes: {timeSpan.Humanize()}");
// Number examples
int number = 12345;
content.Add($"Number 12345 in words: {number.ToWords()}");
content.Add($"Ordinal of 21: {21.ToOrdinalWords()}");
// Pluralization examples
string singular = "car";
content.Add($"Plural of 'car': {singular.Pluralize()}");
string plural = "children";
content.Add($"Singular of 'children': {plural.Singularize()}");
// Byte size examples
long bytes = 1048576;
content.Add($"1,048,576 bytes: {bytes.Bytes().Humanize()}");
return content;
}
}
結論
Humanizerは、.NET開発者が人間に優しく、読みやすい形式で情報を提供するアプリケーションを作成するためには欠かせないライブラリです。 日時の人間化から数値や列挙型のフォーマットまでの幅広い機能が、アプリケーションの使いやすさを向上させるための多用途のツールとなっています。 Humanizerを利用することで、開発者はカスタムフォーマットロジックの実装にかかる時間と労力を節約し、アプリケーションがエンドユーザーにデータをより効果的に伝えることを保証します。
同様に、IronPDFは包括的なPDF生成と操作の機能を提供し、C#プロジェクトでのPDFドキュメントの作成と処理に最適です。 HumanizerとIronPDFを組み合わせることで、.NETアプリケーションの機能とプレゼンテーションが大幅に向上します。 IronPDFのライセンスについての詳細は、IronPDFライセンス情報を参照してください。 詳しくは、HTMLからPDFへの変換に関する詳細チュートリアルをチェックしてください。
よくある質問
C# における Humanizer ライブラリの目的は何ですか?
C# の Humanizer ライブラリは、データを人間が理解しやすい形式に変換することを目的としています。たとえば、日付を相対時間文字列に変換することや、単語の複数形化、数字を単語としてフォーマットすること、列挙型を扱うことなどがあります。これにより、開発者はデータをより読みやすくアクセスしやすい形で提示することができます。
C# で DateTime を相対時間文字列に変換するにはどうすればよいですか?
DateTime オブジェクトを相対時間の文字列に変換するには、Humanizer の Humanize メソッドを使用します。例えば「3 日前」や「5 時間後」のように表示されます。
C# プロジェクトに Humanizer ライブラリをインストールする方法は?
C# プロジェクトに Humanizer ライブラリをインストールするには、NuGet パッケージ マネージャー コンソールで Install-Package Humanizer のコマンドを使用するか、.NET Core CLI で dotnet add package Humanizer を使用します。
Humanizer で可能なデータ変換の例をいくつか教えてください。
Humanizer はいくつかのデータ変換を実行できます。たとえば、Pascal ケースの文字列を文章に変換したり、アンダースコア付き文字列をタイトル ケースに変換したり、長いテキストを指定の長さに切り詰めたりします。
C# の単語の複数形化を Humanizer で支援できますか?
はい、Humanizer には単語を複数形化および単数形化するためのメソッドがあり、通常形や不規則形の変換に対応しています。例えば「car」を「cars」に、「people」を「person」に変換します。
Humanizer は C# で列挙型をどのように処理しますか?
Humanizerはenum値を人間に読みやすい文字列に変換でき、インターフェースでユーザー向けのラベルを表示しやすくします。
C# PDF ライブラリにはどのような機能がありますか?
IronPDF のような C# PDF ライブラリは、PDF ファイルの作成、読み取り、編集、およびコンテンツ抽出などの機能を提供します。また、HTML を PDF に変換したり、ドキュメントをマージしたり、ウォーターマークを追加することもできます。
プロジェクトに C# PDF ライブラリをインストールするにはどうすれば良いですか?
C# PDF ライブラリをインストールするには、「ブラウズ」タブでライブラリ名(例:IronPDF)を検索し、「インストール」をクリックします。
C# で Humanizer と PDF ライブラリを組み合わせることの利点は何ですか?
Humanizer と IronPDF のような PDF ライブラリを組み合わせることで、Humanizer によって人間が読みやすいコンテンツを生成し、それを PDF ドキュメントに変換できるため、ユーザーフレンドリーな PDF レポートやドキュメントを作成するのが容易になります。
Humanizer 使用時のパフォーマンスについて考慮すべきことは何ですか?
Humanizer は効率的に設計されていますが、大規模なデータセットやリアルタイム処理が必要なアプリケーションでは、頻繁なヒューマニゼーション操作の影響を考慮する必要があります。








