SkillAgentSearch skills...

ZString

Zero Allocation StringBuilder for .NET and Unity.

Install / Use

/learn @Cysharp/ZString
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

ZString

GitHub Actions Releases

Zero Allocation StringBuilder for .NET Core and Unity.

  • Struct StringBuilder to avoid allocation of builder itself
  • Rent write buffer from ThreadStatic or ArrayPool
  • All append methods are generics(Append<T>(T value)) and write to buffer directly instead of concatenate value.ToString
  • T1~T16 AppendFormat(AppendFormat<T1,...,T16>(string format, T1 arg1, ..., T16 arg16) avoids boxing of struct argument
  • Also T1~T16 Concat(Concat<T1,...,T16>(T1 arg1, ..., T16 arg16)) avoid boxing and value.ToString allocation
  • Convenient ZString.Format/Concat/Join methods can replace instead of String.Format/Concat/Join
  • Can build both Utf16(Span<char>) and Utf8(Span<byte>) directly
  • Can use inner buffer to avoid allocate final string
  • Integrated with Unity TextMeshPro to avoid string allocation

image

This graph compares following codes.

  • "x:" + x + " y:" + y + " z:" + z
  • ZString.Concat("x:", x, " y:", y, " z:", z)
  • string.Format("x:{0} y:{1} z:{2}", x, y, z)
  • ZString.Format("x:{0} y:{1} z:{2}", x, y, z)
  • new StringBuilder(), Append(), .ToString()
  • ZString.CreateStringBuilder(), Append(), .ToString()

"x:" + x + " y:" + y + " z:" + z is converted to String.Concat(new []{ "x:", x.ToString(), " y:", y.ToString(), " z:", z.ToString() }) by C# compiler. It has each .ToString allocation and params array allocation. string.Format calls String.Format(string, object, object, object) so each arguments causes int -> object boxing.

All ZString methods only allocate final string. Also, ZString has enabled to access inner buffer so if output target has stringless api(like Unity TextMeshPro's SetCharArray), you can achieve completely zero allocation.

The blog post of detailed explanation by author: medium@neuecc/ZString

Related project for loggin using with ZString, Cysharp/ZLogger - Zero Allocation Text/Strcutured Logger.

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

Table of Contents

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Getting Started

For .NET Core, use NuGet.

PM> Install-Package ZString

For Unity, check the releases page, download ZString.Unity.unitypackage.

using Cysharp.Text; // namespace

async void Example(int x, int y, int z)
{
    // same as x + y + z
    _ = ZString.Concat(x, y, z);

    // also can use numeric format strings
    _ = ZString.Format("x:{0}, y:{1:000}, z:{2:P}",x, y, z);

    _ = ZString.Join(',', x, y, z);

    // for Unity, direct write(avoid string allocation completely) to TextMeshPro
    tmp.SetTextFormat("Position: {0}, {1}, {2}", x, y, z);

    // create StringBuilder
    using(var sb = ZString.CreateStringBuilder())
    {
        sb.Append("foo");
        sb.AppendLine(42);
        sb.AppendFormat("{0} {1:.###}", "bar", 123.456789);

        // and build final string
        var str = sb.ToString();

        // for Unity, direct write to TextMeshPro
        tmp.SetText(sb);

        // write to destination buffer
        sb.TryCopyTo(dest, out var written);
    }

    // prepare format, return value should store to field(like RegexOptions.Compile)
    var prepared = ZString.PrepareUtf16<int, int>("x:{0}, y:{1:000}");
    _ = prepared.Format(10, 20);

    // C# 8.0, Using declarations
    // create Utf8 StringBuilder that build Utf8 directly to avoid encoding
    using var sb2 = ZString.CreateUtf8StringBuilder();

    sb2.AppendFormat("foo:{0} bar:{1}", x, y);

    // directly write to steam or dest to avoid allocation
    await sb2.WriteToAsync(stream);
    sb2.CopyTo(bufferWritter);
    sb2.TryCopyTo(dest, out var written);
}

Reference

static class ZString

| method | returns | description | | -- | -- | -- | | CreateStringBuilder() | Utf16ValueStringBuilder | Create the Utf16 string StringBuilder. | | CreateStringBuilder(bool notNested) | Utf16ValueStringBuilder | Create the Utf16 string StringBuilder, when true uses thread-static buffer that is faster but must return immediately. | | CreateUtf8StringBuilder() | Utf8ValueStringBuilder | Create the Utf8(Span<byte>) StringBuilder. | | CreateUtf8StringBuilder(bool notNested) | Utf8ValueStringBuilder | Create the Utf8(Span<byte>) StringBuilder, when true uses thread-static buffer that is faster but must return immediately. | | Join(char/string, T[]/IE<T>) | string | Concatenates the elements of an array, using the specified seperator between each element. | | PrepareUtf16<T1,..,T16>(string) | Utf16PreparedFormat<T1,...,T16> | Prepare string format to avoid parse template in each operation. | | PrepareUtf8<T1,..,T16>(string) | Utf8PreparedFormat<T1,...,T16> | Prepare string format to avoid parse template in each operation. | | Concat<T1,..,T16>(T1,..,T16) | string | Concatenates the string representation of some specified values. | | Format<T1,..,T16>(string, T1,..,T16) | string | Replaces one or more format items in a string with the string representation of some specified values. | | Utf8Format<T1,..,T16>(IBufferWriter<byte>, T1,..,T16) | void | Replaces one or more format items in a string with the string representation of some specified values. |

struct Utf16ValueStringBuilder : IBufferWriter<char>, IDisposable

| method | returns | description | | -- | -- | -- | | Length | int | Length of written buffer. | | AsSpan() | ReadOnlySpan<char> | Get the written buffer data. | | AsMemory() | ReadOnlyMemory<char> | Get the written buffer data. | | AsArraySegment() | ArraySegment<char> | Get the written buffer data. | | Dispose() | void | Return the inner buffer to pool. | | Append<T>(T value) | void | Appends the string representation of a specified value to this instance. | | Append<T>(T value, string format) | void | Appends the string representation of a specified value to this instance with numeric format strings. | | AppendJoin(char/string, T[]/IE<T>)| void | Concatenates and appends the elements of an array, using the specified seperator between each element. | | AppendLine() | void | Appends the default line terminator to the end of this instance. | | AppendLine<T>(T value) | void | Appends the string representation of a specified value followed by the default line terminator to the end of this instance. | | AppendLine<T>(T value, string format) | void | Appends the string representation of a specified value with numeric format strings followed by the default line terminator to the end of this instance. | | AppendFormat<T1,..,T16>(string, T1,..,T16) | void | Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. | | TryCopyTo(Span<char>, out int) | bool | Copy inner buffer to the destination span. | | ToString() | string | Converts the value of this instance to a System.String. | | GetMemory(int sizeHint) | Memory<char> | IBufferWriter.GetMemory. | | GetSpan(int sizeHint) | Span<char> | IBufferWriter.GetSpan. | | Advance(int count) | void | IBufferWriter.Advance. | | static RegisterTryFormat<T>(TryFormat<T>) | void | Register custom formatter. |

struct Utf8ValueStringBuilder : IBufferWriter<byte>, IDisposable

| method | returns | description | | -- | -- | -- | | Length | int | Length of written buffer. | | AsSpan() | ReadOnlySpan<char> | Get the written buffer data. | | AsMemory() | ReadOnlyMemory<char> | Get the written buffer data. | | AsArraySegment() | ArraySegment<char> | Get the written buffer data. | | Dispose() | void | Return the inner buffer to pool. | | Append<T>(T value) | void | Appends the string representation of a specified value to this instance. | | Append<T>(T value, StandardFormat format) | void | Appends the string representation of a specified value to this instance with numeric format strings. | | AppendJoin(char/string, T[]/IE<T>)| void | Concatenates and appends the elements of an array, using the specified seperator between each element. | | AppendLine() | void | Appends the default line terminator to the end of this instance. | | AppendLine<T>(T value) | void | Appends the string representation of a specified value followed by the default line terminator to the end of this instance. | | AppendLine<T>(T value, StandardFormat format) | void | Appends the string representation of a specified value with numeric format strings followed by the default line terminator to the end of this instance. | | AppendFormat<T1,..,T16>(string, T1,..,T16) | void | Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. | | CopyTo(IBufferWriter<byte>) | void | Copy inner buffer to the buffer writer. | | TryCopyTo(Span<byte>, out int) | bool | Copy inner buffer to the destination span. | | WriteToAsync(Stream stream) | Task | Write inner buffer to stream. | | ToString() | string | Encode the innner utf8 buffer to a System.String. | | GetMemory(int sizeHint) | Memory<char> | IBufferWriter.GetMemory. | | GetSpan(int sizeHint) | Span<char> | IBufferWriter.GetSpan. | | Advance(int count) | void | IBufferWriter.Ad

View on GitHub
GitHub Stars2.7k
CategoryDevelopment
Updated15h ago
Forks183

Languages

C#

Security Score

95/100

Audited on Mar 27, 2026

No findings