Table of Contents

Class XString

Namespace
Prefrontal.Common.Extensions
Assembly
Prefrontal.Core.dll

Extension methods for string.

public static class XString
Inheritance
XString
Inherited Members

Methods

After(string, char, bool)

Returns the right hand side of the first occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string After(this string str, char delimiter, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter char

The delimiter to search for.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

After(string, char, int, bool)

Returns the right hand side of the first occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string After(this string str, char delimiter, int startIndex, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter char

The delimiter to search for.

startIndex int

The index in the string to start searching from.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

After(string, string, bool)

Returns the right hand side of the first occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string After(this string str, string delimiter, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter string

The delimiter to search for.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

After(string, string, int, bool)

Returns the right hand side of the first occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string After(this string str, string delimiter, int startIndex, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter string

The delimiter to search for.

startIndex int

The index in the string to start searching from.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

AfterLast(string, char, bool)

Returns the right hand side of the last occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string AfterLast(this string str, char delimiter, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter char

The delimiter to search for.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

AfterLast(string, char, int, bool)

Returns the right hand side of the last occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string AfterLast(this string str, char delimiter, int startIndex, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter char

The delimiter to search for.

startIndex int

The index in the string to start searching from.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

AfterLast(string, string, bool)

Returns the right hand side of the last occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string AfterLast(this string str, string delimiter, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter string

The delimiter to search for.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

AfterLast(string, string, int, bool)

Returns the right hand side of the last occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string AfterLast(this string str, string delimiter, int startIndex, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter string

The delimiter to search for.

startIndex int

The index in the string to start searching from.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

Before(string, char, bool)

Returns the left hand side of the first occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string Before(this string str, char delimiter, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter char

The delimiter to search for.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

Before(string, char, int, bool)

Returns the left hand side of the first occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string Before(this string str, char delimiter, int startIndex, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter char

The delimiter to search for.

startIndex int

The index in the string to start searching from.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

Before(string, string, bool)

Returns the left hand side of the first occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string Before(this string str, string delimiter, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter string

The delimiter to search for.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

Before(string, string, int, bool)

Returns the left hand side of the first occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string Before(this string str, string delimiter, int startIndex, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter string

The delimiter to search for.

startIndex int

The index in the string to start searching from.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

BeforeLast(string, char, bool)

Returns the left hand side of the last occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string BeforeLast(this string str, char delimiter, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter char

The delimiter to search for.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

BeforeLast(string, char, int, bool)

Returns the left hand side of the last occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string BeforeLast(this string str, char delimiter, int startIndex, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter char

The delimiter to search for.

startIndex int

The index in the string to start searching from.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

BeforeLast(string, string, bool)

Returns the left hand side of the last occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string BeforeLast(this string str, string delimiter, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter string

The delimiter to search for.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

BeforeLast(string, string, int, bool)

Returns the left hand side of the last occurrence of delimiter in the string. If the delimiter is not found, the whole string is returned unless returnWholeStringOnDefault set to false.

public static string BeforeLast(this string str, string delimiter, int startIndex, bool returnWholeStringOnDefault = true)

Parameters

str string

The string to search in.

delimiter string

The delimiter to search for.

startIndex int

The index in the string to start searching from.

returnWholeStringOnDefault bool

Whether to return the whole string if the delimiter is not found (true), or an empty string (false).

Returns

string

The left hand side of the delimiter if found, otherwise the whole string or an empty string.

Exceptions

NullReferenceException

If str is null.

NullIfEmpty(string?)

Returns the string itself if it's not null nor empty, otherwise returns null.
Handy for using ?? operator to provide a default value for a string.

public static string? NullIfEmpty(this string? value)

Parameters

value string

Returns

string

NullIfWhiteSpace(string?)

Returns the string itself if it contains any non-whitespace characters, otherwise returns null.
Handy for using ?? operator to provide a default value for a string.

public static string? NullIfWhiteSpace(this string? value)

Parameters

value string

Returns

string

SplitAndWrapLines(string, int, string, string)

Splits a string into lines (by lineEnding) and wraps the lines at maxLineLength. This method makes sure that words are not split between lines. The lines are trimmed of leading and trailing whitespace and then prefixed with linePrefix.

public static IEnumerable<string> SplitAndWrapLines(this string value, int maxLineLength, string lineEnding = "\n", string linePrefix = "")

Parameters

value string

The string to split and wrap.

maxLineLength int

The maximum length of a line.

lineEnding string

The line ending to split the string by.

linePrefix string

The prefix to add to each line.

Returns

IEnumerable<string>

An enumerable of lines.

Exceptions

ArgumentException

If value is null.

ArgumentException

If lineEnding is null or empty.

ArgumentOutOfRangeException

If maxLineLength is less than or equal to zero.

WithMaxLength(string, int)

Just like string[..maxLength] except it doesn't throw an exception when maxLength is greater than the string's length and instead returns the original string.

public static string WithMaxLength(this string input, int maxLength)

Parameters

input string

The input string.

maxLength int

The maximum length of the returned substring.

Returns

string

input with length capped at maxLength.

Exceptions

NullReferenceException

If input is null.

ArgumentOutOfRangeException

If maxLength is less than zero.

WithMaxLengthSuffixed(string, int, string)

A version of WithMaxLength(string, int) that includes the given suffix at the end of the resulting string when input exceeds maxLength.
Note that the returned string will still be at most maxLength characters long.

public static string WithMaxLengthSuffixed(this string input, int maxLength, string suffix = "…")

Parameters

input string

The input string.

maxLength int

The maximum length of the returned substring.

suffix string

The suffix of the resulting string if input exceeds maxLength.

Returns

string

input with length capped at maxLength.

Exceptions

NullReferenceException

If input is null.

ArgumentOutOfRangeException

If maxLength is less than zero.

WrapInsideBox(string, int, int, int, int, string, string, char, char, char, char, char, char)

Wraps the string inside a box with the given dimensions and style.
Example:

var wrapped = "Hello world!".WrapInsideBox(9);
Console.WriteLine(wrapped);
// Output:
// ╭────────╮
// │ Hello  │
// │ world! │
// ╰────────╯
public static string WrapInsideBox(this string value, int maxWidth, int minWidth = 0, int horizontalPadding = 1, int verticalPadding = 0, string linePrefix = "", string lineEnding = "\n", char horizontal = '─', char vertical = '│', char topLeft = '╭', char topRight = '╮', char bottomLeft = '╰', char bottomRight = '╯')

Parameters

value string

The string to wrap inside a box.

maxWidth int

The maximum total width of the box (including the border).

minWidth int

The minimum total width of the box (including the border).

horizontalPadding int

The number of spaces to pad the content with on the left and right within the box.

verticalPadding int

The number of empty lines to add above and below the content within the box.

linePrefix string

The string to prepend to each line. Useful for indentation.

lineEnding string

The newline character used (you'll most likely not need to edit this).

horizontal char

The character to use for the horizontal border.

vertical char

The character to use for the vertical border.

topLeft char

The character to use for the top left corner of the box.

topRight char

The character to use for the top right corner of the box.

bottomLeft char

The character to use for the bottom left corner of the box.

bottomRight char

The character to use for the bottom right corner of the box.

Returns

string

The string wrapped inside a box.

Exceptions

ArgumentException

If value is null.