Philiprehberger.TextDiff 0.2.1

dotnet add package Philiprehberger.TextDiff --version 0.2.1
                    
NuGet\Install-Package Philiprehberger.TextDiff -Version 0.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Philiprehberger.TextDiff" Version="0.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.TextDiff" Version="0.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Philiprehberger.TextDiff" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.TextDiff --version 0.2.1
                    
#r "nuget: Philiprehberger.TextDiff, 0.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Philiprehberger.TextDiff@0.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.TextDiff&version=0.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Philiprehberger.TextDiff&version=0.2.1
                    
Install as a Cake Tool

Philiprehberger.TextDiff

CI NuGet Last updated

Line-by-line text diff using Myers' algorithm with unified diff output and structured DiffResult objects.

Installation

dotnet add package Philiprehberger.TextDiff

Usage

using Philiprehberger.TextDiff;

var result = Diff.Compare("hello\nworld", "hello\nearth");
Console.WriteLine($"Changes: {result.HasChanges}, Added: {result.AddedCount}, Removed: {result.RemovedCount}");

Unified Diff Output

using Philiprehberger.TextDiff;

var oldText = "alpha\nbeta\ngamma\ndelta";
var newText = "alpha\nbeta\nepsilon\ndelta";

var unified = Diff.Unified(oldText, newText, "file-v1.txt", "file-v2.txt", context: 2);
Console.WriteLine(unified);
// --- file-v1.txt
// +++ file-v2.txt
// @@ -1,4 +1,4 @@
//  alpha
//  beta
// -gamma
// +epsilon
//  delta

Diff Statistics

using Philiprehberger.TextDiff;

var result = Diff.Compare("a\nb\nc", "a\nx\nc");

Console.WriteLine($"Additions: {result.Statistics.Additions}");
Console.WriteLine($"Deletions: {result.Statistics.Deletions}");
Console.WriteLine($"Modifications: {result.Statistics.Modifications}");
Console.WriteLine($"Change %: {result.Statistics.ChangePercentage}");

Diff Options

using Philiprehberger.TextDiff;

var options = new DiffOptions
{
    IgnoreWhitespace = true,
    IgnoreCase = true,
    IgnoreBlankLines = true
};

var result = Diff.Compare("  Hello  \n\nWorld", "hello\nworld", options);
Console.WriteLine($"Has changes: {result.HasChanges}"); // False

Character-Level Inline Diff

using Philiprehberger.TextDiff;

var changes = InlineDiff.ComputeCharacterDiff("hello world", "hello earth");

foreach (var change in changes)
{
    var marker = change.Type switch
    {
        ChangeType.Equal => " ",
        ChangeType.Delete => "-",
        ChangeType.Insert => "+"
    };
    Console.Write($"[{marker}{change.Text}]");
}
// [ hello ][- ][+e][-w][-o][-r][-l][-d][+a][+r][+t][+h]

Word-Level Diff

using Philiprehberger.TextDiff;

var changes = InlineDiff.ComputeWordDiff("the quick brown fox", "the slow brown cat");

foreach (var change in changes)
{
    if (change.Type == ChangeType.Delete)
        Console.Write($"[-{change.Text}]");
    else if (change.Type == ChangeType.Insert)
        Console.Write($"[+{change.Text}]");
    else
        Console.Write(change.Text);
}
// the [-quick][+slow] brown [-fox][+cat]

API

Diff

Method Description
Compare(oldText, newText, options?) Compares two texts and returns a DiffResult with lines, counts, and statistics
Unified(oldText, newText, oldLabel?, newLabel?, context?, options?) Produces a unified diff string with ---, +++, and @@ headers
Lines(oldText, newText, options?) Returns a list of DiffLine entries with change types and line numbers

DiffResult

Property Type Description
Lines IReadOnlyList<DiffLine> The individual diff lines
AddedCount int Number of added lines
RemovedCount int Number of removed lines
UnchangedCount int Number of unchanged lines
HasChanges bool Whether the texts differ
Statistics DiffStatistics Detailed diff statistics

DiffLine

Property Type Description
Type DiffLineType Added, Removed, or Unchanged
Content string The text content of the line
OldLineNumber int? 1-based line number in the old text
NewLineNumber int? 1-based line number in the new text

DiffStatistics

Property Type Description
Additions int Number of added lines
Deletions int Number of deleted lines
Modifications int Number of modified lines (paired remove/add sequences)
ChangePercentage double Percentage of lines that changed (0.0 to 100.0)

DiffOptions

Property Type Default Description
IgnoreWhitespace bool false Ignore leading and trailing whitespace
IgnoreCase bool false Ignore case differences
IgnoreBlankLines bool false Skip blank lines when comparing
Mode DiffMode Line Diff granularity: Line, Word, or Character

InlineDiff

Method Description
ComputeCharacterDiff(oldText, newText) Character-level diff returning InlineChange segments
ComputeWordDiff(oldText, newText) Word-level diff returning InlineChange segments

InlineChange

Property Type Description
Type ChangeType Equal, Insert, or Delete
Text string The text content of the segment

DiffMode

Value Description
Line Compare text line by line
Word Compare text word by word
Character Compare text character by character

Development

dotnet build src/Philiprehberger.TextDiff.csproj --configuration Release

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.1 90 4/1/2026
0.2.0 95 3/28/2026
0.1.1 89 3/23/2026
0.1.0 87 3/21/2026