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
stringThe string to search in.
delimiter
charThe delimiter to search for.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
charThe delimiter to search for.
startIndex
intThe index in the string to start searching from.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
stringThe delimiter to search for.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
stringThe delimiter to search for.
startIndex
intThe index in the string to start searching from.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
charThe delimiter to search for.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
charThe delimiter to search for.
startIndex
intThe index in the string to start searching from.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
stringThe delimiter to search for.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
stringThe delimiter to search for.
startIndex
intThe index in the string to start searching from.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
charThe delimiter to search for.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
charThe delimiter to search for.
startIndex
intThe index in the string to start searching from.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
stringThe delimiter to search for.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
stringThe delimiter to search for.
startIndex
intThe index in the string to start searching from.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
charThe delimiter to search for.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
charThe delimiter to search for.
startIndex
intThe index in the string to start searching from.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
stringThe delimiter to search for.
returnWholeStringOnDefault
boolWhether 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
stringThe string to search in.
delimiter
stringThe delimiter to search for.
startIndex
intThe index in the string to start searching from.
returnWholeStringOnDefault
boolWhether 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
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
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
stringThe string to split and wrap.
maxLineLength
intThe maximum length of a line.
lineEnding
stringThe line ending to split the string by.
linePrefix
stringThe 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
Returns
- string
input
with length capped atmaxLength
.
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
stringThe input string.
maxLength
intThe maximum length of the returned substring.
suffix
stringThe suffix of the resulting string if
input
exceedsmaxLength
.
Returns
- string
input
with length capped atmaxLength
.
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
stringThe string to wrap inside a box.
maxWidth
intThe maximum total width of the box (including the border).
minWidth
intThe minimum total width of the box (including the border).
horizontalPadding
intThe number of spaces to pad the content with on the left and right within the box.
verticalPadding
intThe number of empty lines to add above and below the content within the box.
linePrefix
stringThe string to prepend to each line. Useful for indentation.
lineEnding
stringThe newline character used (you'll most likely not need to edit this).
horizontal
charThe character to use for the horizontal border.
vertical
charThe character to use for the vertical border.
topLeft
charThe character to use for the top left corner of the box.
topRight
charThe character to use for the top right corner of the box.
bottomLeft
charThe character to use for the bottom left corner of the box.
bottomRight
charThe 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.