Skip to content

OnixLabs.Core.StringExtensions

Matthew Layton edited this page Nov 15, 2026 · 2 revisions

StringExtensions Class

Extension methods for string instances, covering nth-occurrence indexing, delimiter-based substring extraction, repetition, wrapping, encoding/parsing conversions, and escaping.

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

Definition

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

Note This class is decorated with [EditorBrowsable(EditorBrowsableState.Never)], which keeps the container class itself out of IntelliSense lists while its extension methods remain fully usable on the receiver types. Bring the methods into scope with using OnixLabs.Core;.

Remarks

All comparison-based methods default to StringComparison.Ordinal for predictable, culture-independent matching. Pass an explicit StringComparison argument to any overload that accepts one to override this behaviour.

Methods

Method Returns Description
NthIndexOf(char, int) int Index of the nth occurrence of a character, or -1.
NthIndexOf(string, int, StringComparison) int Index of the nth occurrence of a substring, or -1.
Repeat(int) string Repeats the string the specified number of times.
SubstringBeforeFirst(char, string?) string Sub-string before the first occurrence of a character.
SubstringBeforeFirst(string, string?, StringComparison) string Sub-string before the first occurrence of a substring.
SubstringBeforeLast(char, string?) string Sub-string before the last occurrence of a character.
SubstringBeforeLast(string, string?, StringComparison) string Sub-string before the last occurrence of a substring.
SubstringAfterFirst(char, string?) string Sub-string after the first occurrence of a character.
SubstringAfterFirst(string, string?, StringComparison) string Sub-string after the first occurrence of a substring.
SubstringAfterLast(char, string?) string Sub-string after the last occurrence of a character.
SubstringAfterLast(string, string?, StringComparison) string Sub-string after the last occurrence of a substring.
SubstringBeforeNth(char, int, string?) string Sub-string before the nth occurrence of a character.
SubstringBeforeNth(string, int, string?, StringComparison) string Sub-string before the nth occurrence of a substring.
SubstringAfterNth(char, int, string?) string Sub-string after the nth occurrence of a character.
SubstringAfterNth(string, int, string?, StringComparison) string Sub-string after the nth occurrence of a substring.
ToByteArray(Encoding?) byte[] Encodes the string into a byte[].
ToDateTime(IFormatProvider?, DateTimeStyles) DateTime Parses the string into a DateTime.
ToDateOnly(IFormatProvider?, DateTimeStyles) DateOnly Parses the string into a DateOnly.
ToTimeOnly(IFormatProvider?, DateTimeStyles) TimeOnly Parses the string into a TimeOnly.
ToEscapedString() string Formats escape characters as escape character literals.
TryCopyTo(Span<char>, out int) bool Tries to copy the string into a destination span.
Wrap(char, char) string Wraps the string between two characters.
Wrap(string, string) string Wraps the string between two substrings.

NthIndexOf(char, int)

public int NthIndexOf(char seekValue, int count)

Obtains the zero-based index of the nth occurrence of the specified character in this instance. Returns -1 if the receiver is null or empty, if count is less than or equal to zero, or if the specified occurrence does not exist.

Parameter Type Description
seekValue char The character to seek.
count int The 1-based occurrence number to locate.

Returns — The zero-based index of the nth occurrence of seekValue, if found; otherwise, -1.

int index = "a.b.c.d".NthIndexOf('.', 2); // 3

NthIndexOf(string, int, StringComparison)

public int NthIndexOf(string seekValue, int count, StringComparison comparison = StringComparison.Ordinal)

Obtains the zero-based index of the nth occurrence of the specified substring in this instance. Returns -1 if the receiver or seekValue is null or empty, if count is less than or equal to zero, or if the specified occurrence does not exist.

Parameter Type Description
seekValue string The substring to seek.
count int The 1-based occurrence number to locate.
comparison StringComparison The comparison used to compare the receiver and seekValue. Defaults to StringComparison.Ordinal.

Returns — The zero-based index of the nth occurrence of seekValue, if found; otherwise, -1.

Repeat(int)

public string Repeat(int count)

Repeats the current string by the specified number of repetitions. Returns string.Empty when count is less than or equal to zero.

Parameter Type Description
count int The number of repetitions of the current string.

Returns — A new string representing the repetition of the current string.

Exceptions

Exception Condition
OverflowException The length of the result would exceed int.MaxValue.
string line = "ab".Repeat(3); // "ababab"

SubstringBeforeFirst(char, string?)

public string SubstringBeforeFirst(char delimiter, string? defaultValue = null)

Obtains a sub-string before the first occurrence of the specified delimiter.

Parameter Type Description
delimiter char The delimiter to find within the current string.
defaultValue string? The value to return when the delimiter is not found. If null, the current string is returned.

Returns — The sub-string before the first occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.

string name = "user@example.com".SubstringBeforeFirst('@'); // "user"

SubstringBeforeFirst(string, string?, StringComparison)

public string SubstringBeforeFirst(string delimiter, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)

Obtains a sub-string before the first occurrence of the specified delimiter substring.

Parameter Type Description
delimiter string The delimiter to find within the current string.
defaultValue string? The value to return when the delimiter is not found. If null, the current string is returned.
comparison StringComparison The culture, case and sort rules applied when searching. Defaults to StringComparison.Ordinal.

Returns — The sub-string before the first occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.

SubstringBeforeLast(char, string?)

public string SubstringBeforeLast(char delimiter, string? defaultValue = null)

Obtains a sub-string before the last occurrence of the specified delimiter.

Parameter Type Description
delimiter char The delimiter to find within the current string.
defaultValue string? The value to return when the delimiter is not found. If null, the current string is returned.

Returns — The sub-string before the last occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.

string path = "a/b/c/file.txt".SubstringBeforeLast('/'); // "a/b/c"

SubstringBeforeLast(string, string?, StringComparison)

public string SubstringBeforeLast(string delimiter, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)

Obtains a sub-string before the last occurrence of the specified delimiter substring.

Parameter Type Description
delimiter string The delimiter to find within the current string.
defaultValue string? The value to return when the delimiter is not found. If null, the current string is returned.
comparison StringComparison The culture, case and sort rules applied when searching. Defaults to StringComparison.Ordinal.

Returns — The sub-string before the last occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.

SubstringAfterFirst(char, string?)

public string SubstringAfterFirst(char delimiter, string? defaultValue = null)

Obtains a sub-string after the first occurrence of the specified delimiter.

Parameter Type Description
delimiter char The delimiter to find within the current string.
defaultValue string? The value to return when the delimiter is not found. If null, the current string is returned.

Returns — The sub-string after the first occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.

string domain = "user@example.com".SubstringAfterFirst('@'); // "example.com"

SubstringAfterFirst(string, string?, StringComparison)

public string SubstringAfterFirst(string delimiter, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)

Obtains a sub-string after the first occurrence of the specified delimiter substring.

Parameter Type Description
delimiter string The delimiter to find within the current string.
defaultValue string? The value to return when the delimiter is not found. If null, the current string is returned.
comparison StringComparison The culture, case and sort rules applied when searching. Defaults to StringComparison.Ordinal.

Returns — The sub-string after the first occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.

SubstringAfterLast(char, string?)

public string SubstringAfterLast(char delimiter, string? defaultValue = null)

Obtains a sub-string after the last occurrence of the specified delimiter.

Parameter Type Description
delimiter char The delimiter to find within the current string.
defaultValue string? The value to return when the delimiter is not found. If null, the current string is returned.

Returns — The sub-string after the last occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.

string extension = "archive.tar.gz".SubstringAfterLast('.'); // "gz"

SubstringAfterLast(string, string?, StringComparison)

public string SubstringAfterLast(string delimiter, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)

Obtains a sub-string after the last occurrence of the specified delimiter substring.

Parameter Type Description
delimiter string The delimiter to find within the current string.
defaultValue string? The value to return when the delimiter is not found. If null, the current string is returned.
comparison StringComparison The culture, case and sort rules applied when searching. Defaults to StringComparison.Ordinal.

Returns — The sub-string after the last occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.

SubstringBeforeNth(char, int, string?)

public string SubstringBeforeNth(char seekValue, int count, string? defaultValue = null)

Obtains a sub-string before the nth occurrence of the specified character. If the nth occurrence is not found, returns defaultValue, or the entire string when defaultValue is null.

Parameter Type Description
seekValue char The character to find the nth occurrence of.
count int The nth occurrence to find.
defaultValue string? The value to return if the nth occurrence is not found. If null, the current string is returned.

Returns — The sub-string before the nth occurrence of seekValue if found; otherwise, defaultValue or the entire string when defaultValue is null.

string head = "a.b.c.d".SubstringBeforeNth('.', 2); // "a.b"

SubstringBeforeNth(string, int, string?, StringComparison)

public string SubstringBeforeNth(string seekValue, int count, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)

Obtains a sub-string before the nth occurrence of the specified substring. If the nth occurrence is not found, returns defaultValue, or the entire string when defaultValue is null.

Parameter Type Description
seekValue string The substring to find the nth occurrence of.
count int The nth occurrence to find.
defaultValue string? The value to return if the nth occurrence is not found. If null, the current string is returned.
comparison StringComparison The comparison used to compare the receiver and seekValue. Defaults to StringComparison.Ordinal.

Returns — The sub-string before the nth occurrence of seekValue if found; otherwise, defaultValue or the entire string when defaultValue is null.

SubstringAfterNth(char, int, string?)

public string SubstringAfterNth(char seekValue, int count, string? defaultValue = null)

Obtains a sub-string after the nth occurrence of the specified character. If the nth occurrence is not found, returns defaultValue, or the entire string when defaultValue is null.

Parameter Type Description
seekValue char The character to find the nth occurrence of.
count int The nth occurrence to find.
defaultValue string? The value to return if the nth occurrence is not found. If null, the current string is returned.

Returns — The sub-string after the nth occurrence of seekValue if found; otherwise, defaultValue or the entire string when defaultValue is null.

string tail = "a.b.c.d".SubstringAfterNth('.', 2); // "c.d"

SubstringAfterNth(string, int, string?, StringComparison)

public string SubstringAfterNth(string seekValue, int count, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)

Obtains a sub-string after the nth occurrence of the specified substring. If the nth occurrence is not found, returns defaultValue, or the entire string when defaultValue is null.

Parameter Type Description
seekValue string The substring to find the nth occurrence of.
count int The nth occurrence to find.
defaultValue string? The value to return if the nth occurrence is not found. If null, the current string is returned.
comparison StringComparison The comparison used to compare the receiver and seekValue. Defaults to StringComparison.Ordinal.

Returns — The sub-string after the nth occurrence of seekValue if found; otherwise, defaultValue or the entire string when defaultValue is null.

ToByteArray(Encoding?)

public byte[] ToByteArray(Encoding? encoding = null)

Converts the current string instance into a new byte[] array. When encoding is null, a default encoding is used.

Parameter Type Description
encoding Encoding? The encoding used to convert the current string into a byte[]. Defaults to null.

Returns — A new byte[] representation of the current string.

byte[] bytes = "Hello".ToByteArray(Encoding.UTF8);

ToDateTime(IFormatProvider?, DateTimeStyles)

public DateTime ToDateTime(IFormatProvider? provider = null, DateTimeStyles styles = DateTimeStyles.None)

Converts the current string instance into a new DateTime instance via DateTime.Parse.

Parameter Type Description
provider IFormatProvider? An object that provides culture-specific formatting information. Defaults to null.
styles DateTimeStyles A bit-wise combination of values that indicate the permitted style elements. Defaults to DateTimeStyles.None.

Returns — A new DateTime instance parsed from the current string.

Exceptions

Exception Condition
FormatException The current string is not a recognised DateTime.

ToDateOnly(IFormatProvider?, DateTimeStyles)

public DateOnly ToDateOnly(IFormatProvider? provider = null, DateTimeStyles styles = DateTimeStyles.None)

Converts the current string instance into a new DateOnly instance via DateOnly.Parse.

Parameter Type Description
provider IFormatProvider? An object that provides culture-specific formatting information. Defaults to null.
styles DateTimeStyles A bit-wise combination of values that indicate the permitted style elements. Defaults to DateTimeStyles.None.

Returns — A new DateOnly instance parsed from the current string.

Exceptions

Exception Condition
FormatException The current string is not a recognised DateOnly.

ToTimeOnly(IFormatProvider?, DateTimeStyles)

public TimeOnly ToTimeOnly(IFormatProvider? provider = null, DateTimeStyles styles = DateTimeStyles.None)

Converts the current string instance into a new TimeOnly instance via TimeOnly.Parse.

Parameter Type Description
provider IFormatProvider? An object that provides culture-specific formatting information. Defaults to null.
styles DateTimeStyles A bit-wise combination of values that indicate the permitted style elements. Defaults to DateTimeStyles.None.

Returns — A new TimeOnly instance parsed from the current string.

Exceptions

Exception Condition
FormatException The current string is not a recognised TimeOnly.

ToEscapedString()

public string ToEscapedString()

Obtains a string with all escape characters formatted as escape character literals. Newline, carriage return, tab, double quote, single quote and backslash are rendered as their escape sequences; other control characters are rendered as \uXXXX.

Returns — A string with all escape characters formatted as escape character literals.

string escaped = "line1\nline2\t\"quoted\"".ToEscapedString();
// "line1\\nline2\\t\\\"quoted\\\""

TryCopyTo(Span<char>, out int)

public bool TryCopyTo(Span<char> destination, out int charsWritten)

Tries to copy the current string instance into the destination Span<char>.

Parameter Type Description
destination Span<char> The span into which the current string contents are copied.
charsWritten out int The number of characters written to destination; 0 when the copy fails.

Returns — true if the current string was copied into destination; otherwise, false if the destination was too short.

Wrap(char, char)

public string Wrap(char before, char after)

Wraps the current string instance between the specified before and after char instances.

Parameter Type Description
before char The char that should precede the current string.
after char The char that should succeed the current string.

Returns — A new string representing the current string wrapped between before and after.

string wrapped = "value".Wrap('[', ']'); // "[value]"

Wrap(string, string)

public string Wrap(string before, string after)

Wraps the current string instance between the specified before and after string instances.

Parameter Type Description
before string The string that should precede the current string.
after string The string that should succeed the current string.

Returns — A new string representing the current string wrapped between before and after.

string wrapped = "value".Wrap("<<", ">>"); // "<<value>>"

See Also

ONIXLabs .NET Library


Clone this wiki locally