LinkDotNet.StringBuilder
3.6.0
dotnet add package LinkDotNet.StringBuilder --version 3.6.0
NuGet\Install-Package LinkDotNet.StringBuilder -Version 3.6.0
<PackageReference Include="LinkDotNet.StringBuilder" Version="3.6.0" />
<PackageVersion Include="LinkDotNet.StringBuilder" Version="3.6.0" />
<PackageReference Include="LinkDotNet.StringBuilder" />
paket add LinkDotNet.StringBuilder --version 3.6.0
#r "nuget: LinkDotNet.StringBuilder, 3.6.0"
#:package LinkDotNet.StringBuilder@3.6.0
#addin nuget:?package=LinkDotNet.StringBuilder&version=3.6.0
#tool nuget:?package=LinkDotNet.StringBuilder&version=3.6.0
StringBuilder
A fast and low allocation StringBuilder for .NET.
Getting Started
Install the package:
PM> Install-Package LinkDotNet.StringBuilder
Afterward, use the package as follow:
using LinkDotNet.StringBuilder; // Namespace of the package
using ValueStringBuilder stringBuilder = new();
stringBuilder.AppendLine("Hello World");
string result = stringBuilder.ToString();
There are also smaller helper functions, which enable you to use ValueStringBuilder without any instance:
string result1 = ValueStringBuilder.Concat("Hello ", "World"); // "Hello World"
string result2 = ValueStringBuilder.Concat("Hello", 1, 2, 3, "!"); // "Hello123!"
By default, ValueStringBuilder uses a rented buffer from ArrayPool<char>.Shared.
You can avoid renting overhead with an initially stack-allocated buffer:
using ValueStringBuilder stringBuilder = new(stackalloc char[128]);
Note that this will prevent you from returning stringBuilder or assigning it to an out parameter.
What does it solve?
The dotnet version of the StringBuilder is an all-purpose version that normally fits a wide variety of needs.
But sometimes, low allocation is key. Therefore I created the ValueStringBuilder. It is not a class but a ref struct that tries to allocate as little as possible.
If you want to know how the ValueStringBuilder works and why it uses allocations and is even faster, check out this blog post.
The blog goes into a bit more in detail about how it works with a simplistic version of the ValueStringBuilder.
What doesn't it solve?
The library is not meant as a general replacement for the StringBuilder built into .NET. You can head over to the documentation and read about the "Known limitations".
The library works best for a small to medium length strings (not hundreds of thousands of characters, even though it can be still faster and performs fewer allocations). At any time, you can convert the ValueStringBuilder to a "normal" StringBuilder and vice versa.
The normal use case is to concatenate strings in a hot path where the goal is to put as minimal pressure on the GC as possible.
Documentation
More detailed documentation can be found here. It is really important to understand how the ValueStringBuilder works so that you did not run into weird situations where performance/allocations can even rise.
Benchmark
The following table compares the built-in StringBuilder and this library's ValueStringBuilder:
BenchmarkDotNet v0.15.8, macOS Sequoia 15.7.7 (24G720) [Darwin 24.6.0]
Apple M2 Pro, 1 CPU, 12 logical and 12 physical cores
.NET SDK 10.0.400
[Host] : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
DefaultJob : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
| Method | Mean | Error | StdDev | Ratio | Gen0 | Allocated | Alloc Ratio |
|-------------------- |----------:|---------:|---------:|------:|-------:|----------:|------------:|
| DotNetStringBuilder | 120.84 ns | 2.093 ns | 1.748 ns | 1.00 | 0.1779 | 1488 B | 1.00 |
| ValueStringBuilder | 90.14 ns | 1.017 ns | 0.901 ns | 0.75 | 0.0669 | 560 B | 0.38 |
For more comparisons, check the documentation.
ValueStringBuilder also avoids boxing value types (int, double, DateTime, Guid, and 16 more) passed to
AppendJoin, Concat, AppendFormat, ReplaceGeneric, and interpolated strings, and vectorizes Trim/TrimStart/TrimEnd
via SearchValues<char>. The following benchmark shows the combined effect against StringBuilder for a few representative
operations:
Operations, top to bottom: concatenating 5 mixed values, joining 10 ints with a separator, an interpolated string with 5 value-type holes, replacing a placeholder with a formatted int, and trimming a padded 1000-char buffer.
BenchmarkDotNet v0.15.8, macOS Sequoia 15.7.7 (24G720) [Darwin 24.6.0]
Apple M2 Pro, 1 CPU, 12 logical and 12 physical cores
.NET SDK 10.0.400
[Host] : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
DefaultJob : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------- |----------:|---------:|---------:|-------:|-------:|----------:|
| StringBuilderConcat | 245.67 ns | 0.456 ns | 0.381 ns | 0.0792 | - | 664 B |
| StringBuilderAppendJoin | 55.29 ns | 0.898 ns | 0.701 ns | 0.0325 | - | 272 B |
| StringBuilderInterpolated | 271.05 ns | 5.366 ns | 6.387 ns | 0.0610 | - | 512 B |
| StringBuilderReplace | 50.69 ns | 0.863 ns | 0.923 ns | 0.0325 | - | 272 B |
| StringBuilderTrim | 920.99 ns | 3.383 ns | 3.165 ns | 0.7629 | 0.0114 | 6384 B |
| ValueStringBuilderConcat | 191.30 ns | 0.971 ns | 0.861 ns | 0.0210 | - | 176 B |
| ValueStringBuilderAppendJoin | 38.77 ns | 0.097 ns | 0.086 ns | 0.0076 | - | 64 B |
| ValueStringBuilderInterpolated | 146.29 ns | 0.462 ns | 0.409 ns | 0.0191 | - | 160 B |
| ValueStringBuilderReplace | 29.46 ns | 0.550 ns | 0.515 ns | 0.0038 | - | 32 B |
| ValueStringBuilderTrim | 124.74 ns | 1.145 ns | 0.956 ns | 0.0057 | - | 48 B |
Comparing each ValueStringBuilder row against its StringBuilder counterpart above:
| Operation | Time | Allocated |
|---|---|---|
| Concat | 0.78x (1.3x faster) | 0.27x (3.8x less) |
| AppendJoin | 0.70x (1.4x faster) | 0.24x (4.3x less) |
| Interpolated | 0.54x (1.9x faster) | 0.31x (3.2x less) |
| Replace | 0.58x (1.7x faster) | 0.12x (8.5x less) |
| Trim | 0.14x (7.4x faster) | 0.01x (133x less) |
Check out the Benchmark for a more detailed comparison and setup.
Support & Contributing
Thanks to all contributors and people that are creating bug-reports and valuable input:
<a href="https://github.com/linkdotnet/StringBuilder/graphs/contributors"> <img src="https://contrib.rocks/image?repo=linkdotnet/StringBuilder" alt="Supporters" /> </a>
| Product | Versions 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 is compatible. 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 is compatible. 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. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (11)
Showing the top 5 NuGet packages that depend on LinkDotNet.StringBuilder:
| Package | Downloads |
|---|---|
|
DccUtils.TypeFormatting
Package Description |
|
|
Reo.Core.Reports
Package Description |
|
|
JsonhCs
JSON for Humans. |
|
|
Vigilance
2D Game Engine |
|
|
SystemCall
A command-parsing library inspired by Jinx. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on LinkDotNet.StringBuilder:
| Repository | Stars |
|---|---|
|
MeltyPlayer/MeltyTool
Multitool for viewing/extracting assets from various N64/GCN/3DS/PC games en-masse.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 3.6.0 | 173 | 9/1/2026 |
| 3.5.0 | 81 | 9/1/2026 |
| 3.4.2 | 11,528 | 7/4/2026 |
| 3.4.1 | 14,616 | 2/19/2026 |
| 3.4.0 | 155 | 2/18/2026 |
| 3.3.0 | 3,383 | 1/19/2026 |
| 3.2.0 | 4,485 | 10/31/2025 |
| 3.1.0 | 244 | 10/31/2025 |
| 3.0.0 | 287 | 10/30/2025 |
| 2.4.1 | 16,985 | 3/25/2025 |
| 2.4.0 | 24,304 | 2/21/2025 |
| 2.3.1 | 291 | 2/20/2025 |
| 2.3.0 | 337 | 2/16/2025 |
| 2.2.0 | 2,587 | 1/25/2025 |
| 2.1.0 | 624 | 1/14/2025 |
| 2.0.0 | 287 | 1/12/2025 |
| 1.22.0 | 496 | 12/18/2024 |
| 1.21.1 | 1,826 | 11/8/2024 |
| 1.21.0 | 2,132 | 9/20/2024 |
| 1.20.0 | 10,869 | 5/2/2024 |