Skip to content

StringExtensions Class

Helpers for strings.

C#
public static class StringExtensions

Inheritance object

Aligns a string given a total character width and alignment style. Fills in the extra space with the space character.

C#
public static string Align(this string value, HorizontalAlignment alignment, int totalWidth)

value string
The current string.

alignment HorizontalAlignment
The horizontal alignment.

totalWidth int
The total width of the new string.

string
A new string instance.

Align(string, HorizontalAlignment, int, char)

Section titled “Align(string, HorizontalAlignment, int, char)”

Aligns a string given a total character width and alignment style.

C#
public static string Align(this string value, HorizontalAlignment alignment, int totalWidth, char fillCharacter)

value string
The current string.

alignment HorizontalAlignment
The horizontal alignment.

totalWidth int
The total width of the new string.

fillCharacter char
The character to use to fill in the extra spaces after alignment.

string
A new string instance.

CreateColored(string, Color?, Color?, Mirror?, CellDecorator[]?)

Section titled “CreateColored(string, Color?, Color?, Mirror?, CellDecorator[]?)”

Creates a ColoredString object from an existing string with the specified foreground and background, setting the ignore properties if needed.

C#
public static ColoredString CreateColored(this string value, Color? foreground = null, Color? background = null, Mirror? mirror = null, CellDecorator[]? decorators = null)

value string
The current string.

foreground System.NullableSadRogue.Primitives.Color
The foreground color. If null, IgnoreForeground will be set.

background System.NullableSadRogue.Primitives.Color
The background color. If null, IgnoreBackground will be set.

mirror Mirror?
The mirror setting. If null, IgnoreMirror will be set.

decorators CellDecorator[]
The decorators setting. If null, IgnoreDecorators will be set.

ColoredString
A ColoredString object instance.

Creates a ColoredString object from an existing string with the specified foreground gradient and cell effect.

C#
public static ColoredString CreateGradient(this string value, Color startingForeground, Color endingForeground)

value string
The current string.

startingForeground SadRogue.Primitives.Color
The starting foreground color to blend.

endingForeground SadRogue.Primitives.Color
The ending foreground color to blend.

ColoredString
A ColoredString object instance.

CreateGradient(string, Color, Color, Color, Color)

Section titled “CreateGradient(string, Color, Color, Color, Color)”

Creates a ColoredString object from an existing string with the specified foreground gradient, background gradient, and cell effect.

C#
public static ColoredString CreateGradient(this string value, Color startingForeground, Color endingForeground, Color startingBackground, Color endingBackground)

value string
The current string.

startingForeground SadRogue.Primitives.Color
The starting foreground color to blend.

endingForeground SadRogue.Primitives.Color
The ending foreground color to blend.

startingBackground SadRogue.Primitives.Color
The starting background color to blend.

endingBackground SadRogue.Primitives.Color
The ending background color to blend.

ColoredString
A ColoredString object instance.

Wraps text into lines by words, long words are also properly wrapped into multiple lines.

C#
public static IEnumerable<string> WordWrap(this string text, int maxCharsPerLine)

text string
The text to parse.

maxCharsPerLine int
The maximum number of characters per line of text returned.

IEnumerable<string>
Each line in the string.

Converts a string to a boolean when it is “0”, “1”, “true”, or “false”.

C#
public static bool ToBool(this string item)

item string
The string to convert

bool
The converted boolean value, otherwise false.

Returns a string of mask characters the same length as the input string.

C#
public static string Masked(this string toMask, char mask)

toMask string
The string to mask.

mask char
The mask to use.

string
A string of masks.