Skip to content

OnixLabs.Core.Text.StringBuilderExtensions

matthew edited this page May 27, 2026 · 1 revision

StringBuilderExtensions Class

Provides extension methods for StringBuilder instances, complementing the built-in Append/Insert API with prepend, trim, and wrap operations. Every method returns the same StringBuilder instance, enabling fluent chaining.

Namespace OnixLabs.Core.Text
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → StringBuilderExtensions

Definition

[EditorBrowsable(EditorBrowsableState.Never)]
public static class StringBuilderExtensions

Note This class is decorated with [EditorBrowsable(EditorBrowsableState.Never)], which hides it from IntelliSense completion lists in some IDE configurations. The extension methods remain fully public and callable; they primarily support the Base value codecs and other internal text formatting throughout the library.

Remarks

StringBuilderExtensions fills gaps in the standard StringBuilder API. All methods return the receiver (this StringBuilder) to support fluent chaining. The Trim family removes repeated leading or trailing occurrences; the Wrap family is a thin convenience around Prepend + Append; and the params-based Append / Prepend overloads delegate to AppendJoin / PrependJoin with an empty separator, so elements are concatenated without any delimiter.

Bring the methods into scope with:

using OnixLabs.Core.Text;

Methods

Method Returns Description
Append(params object[]) StringBuilder Appends the specified values with no separator.
Prepend(object) StringBuilder Prepends the specified object value.
Prepend(char) StringBuilder Prepends the specified char value.
Prepend(string) StringBuilder Prepends the specified string value.
Prepend(params object[]) StringBuilder Prepends the specified values with no separator.
PrependJoin(string, params object?[]) StringBuilder Joins the specified values with a separator and prepends the result.
Trim(char) StringBuilder Trims the specified char from the start and end.
TrimStart(char) StringBuilder Trims the specified char from the start.
TrimEnd(char) StringBuilder Trims the specified char from the end.
Trim(string) StringBuilder Trims the specified string from the start and end.
TrimStart(string) StringBuilder Trims the specified string from the start.
TrimEnd(string) StringBuilder Trims the specified string from the end.
Wrap(char, char) StringBuilder Wraps the builder between the specified start and end char values.
Wrap(string, string) StringBuilder Wraps the builder between the specified start and end string values.

Append(params object[])

public static StringBuilder Append(this StringBuilder receiver, params object[] values)

Appends the specified values to the current StringBuilder, concatenated with no separator between elements (implemented as AppendJoin(string.Empty, values)).

Parameter Type Description
values object[] The values to append.

Returns — The current StringBuilder with the specified values appended.


Prepend(object)

public static StringBuilder Prepend(this StringBuilder receiver, object value)

Prepends the specified object value to the current StringBuilder (inserts at index 0).

Parameter Type Description
value object The value to prepend.

Returns — The current StringBuilder with the specified value prepended.


Prepend(char)

public static StringBuilder Prepend(this StringBuilder receiver, char value)

Prepends the specified char value to the current StringBuilder.

Parameter Type Description
value char The value to prepend.

Returns — The current StringBuilder with the specified value prepended.


Prepend(string)

public static StringBuilder Prepend(this StringBuilder receiver, string value)

Prepends the specified string value to the current StringBuilder.

Parameter Type Description
value string The value to prepend.

Returns — The current StringBuilder with the specified value prepended.


Prepend(params object[])

public static StringBuilder Prepend(this StringBuilder receiver, params object[] values)

Prepends the specified values to the current StringBuilder, concatenated with no separator between elements (implemented as PrependJoin(string.Empty, values)).

Parameter Type Description
values object[] The values to prepend.

Returns — The current StringBuilder with the specified values prepended.


PrependJoin(string, params object?[])

public static StringBuilder PrependJoin(this StringBuilder receiver, string separator, params object?[] values)

Concatenates the string representations of the elements in values, using separator between each member, then prepends the result to the current StringBuilder.

Parameter Type Description
separator string The string to use as a separator. separator is included in the joined string only if values has more than one element.
values object?[] An array containing the values to concatenate and prepend.

Returns — The current StringBuilder with the specified values joined and prepended.


Trim(char)

public static StringBuilder Trim(this StringBuilder receiver, char value)

Trims the specified char value from the start and end of the current StringBuilder.

Parameter Type Description
value char The char value to trim.

Returns — The current StringBuilder with the specified char value trimmed from the start and end.


TrimStart(char)

public static StringBuilder TrimStart(this StringBuilder receiver, char value)

Trims the specified char value from the start of the current StringBuilder. Removes leading occurrences repeatedly until the first character no longer matches value.

Parameter Type Description
value char The char value to trim.

Returns — The current StringBuilder with the specified char value trimmed from the start.


TrimEnd(char)

public static StringBuilder TrimEnd(this StringBuilder receiver, char value)

Trims the specified char value from the end of the current StringBuilder. Removes trailing occurrences repeatedly until the last character no longer matches value.

Parameter Type Description
value char The char value to trim.

Returns — The current StringBuilder with the specified char value trimmed from the end.


Trim(string)

public static StringBuilder Trim(this StringBuilder receiver, string value)

Trims the specified string value from the start and end of the current StringBuilder.

Parameter Type Description
value string The string value to trim.

Returns — The current StringBuilder with the specified string value trimmed from the start and end.


TrimStart(string)

public static StringBuilder TrimStart(this StringBuilder receiver, string value)

Trims the specified string value from the start of the current StringBuilder. Removes leading occurrences repeatedly until the builder no longer begins with value. If value is null or empty, the builder is returned unchanged.

Parameter Type Description
value string The string value to trim.

Returns — The current StringBuilder with the specified string value trimmed from the start.


TrimEnd(string)

public static StringBuilder TrimEnd(this StringBuilder receiver, string value)

Trims the specified string value from the end of the current StringBuilder. Removes trailing occurrences repeatedly until the builder no longer ends with value. If value is null or empty, the builder is returned unchanged.

Parameter Type Description
value string The string value to trim.

Returns — The current StringBuilder with the specified string value trimmed from the end.


Wrap(char, char)

public static StringBuilder Wrap(this StringBuilder receiver, char start, char end)

Wraps the current StringBuilder between the specified start and end char values, prepending start and appending end.

Parameter Type Description
start char The char value to prepend.
end char The char value to append.

Returns — The current StringBuilder wrapped between the specified start and end char values.


Wrap(string, string)

public static StringBuilder Wrap(this StringBuilder receiver, string start, string end)

Wraps the current StringBuilder between the specified start and end string values, prepending start and appending end.

Parameter Type Description
start string The string value to prepend.
end string The string value to append.

Returns — The current StringBuilder wrapped between the specified start and end string values.

using OnixLabs.Core.Text;
using System.Text;

var builder = new StringBuilder("value");

builder.Wrap('[', ']');        // "[value]"
builder.Trim('[').Trim(']');   // "value"
builder.Prepend("key=");       // "key=value"
builder.Append(1, 2, 3);       // "key=value123"

See Also

ONIXLabs .NET Library


Clone this wiki locally