Table of Contents

Class StringConversionExtensions

Namespace
HeroKit.Strings
Assembly
HeroKit.dll

Converts strings to other types.

public static class StringConversionExtensions
Inheritance
StringConversionExtensions
Inherited Members

Remarks

Every conversion comes in two forms. ToX is lenient: it returns a default value for null, empty or invalid input and never throws. TryToX reports success: it returns false for null, empty or invalid input and never throws. Parsing uses the invariant culture unless a format provider is passed.

Methods

FromBase64(string?)

Decodes a base64 string to bytes.

public static byte[]? FromBase64(this string? value)

Parameters

value string

The base64 string.

Returns

byte[]

The decoded bytes, or null if the string is null, empty or not valid base64.

Examples

"SGVyb0tpdA==".FromBase64(); // bytes of "HeroKit"

ToBoolean(string?, string?, bool)

Converts a string to a boolean. Accepts "true"/"false" in any case and an optional custom true text.

public static bool ToBoolean(this string? value, string? trueText = null, bool defaultValue = false)

Parameters

value string

The string to convert.

trueText string

An extra text that means true, compared ignoring case, for example "yes" or "1".

defaultValue bool

The value returned when the string is null or empty.

Returns

bool

The boolean value. Any other non-empty text returns false.

Examples

"TRUE".ToBoolean();               // true
"yes".ToBoolean(trueText: "yes"); // true
"".ToBoolean(defaultValue: true); // true

ToDateTime(string?, DateTime, IFormatProvider?)

Converts a string to a DateTime.

public static DateTime ToDateTime(this string? value, DateTime defaultValue = default, IFormatProvider? provider = null)

Parameters

value string

The string to convert.

defaultValue DateTime

The value returned when the string is null, empty or invalid.

provider IFormatProvider

The format provider. Defaults to InvariantCulture.

Returns

DateTime

The DateTime, or defaultValue.

Examples

"2026-03-07".ToDateTime();                                          // 7 March 2026
"07.03.2026".ToDateTime(provider: CultureInfo.GetCultureInfo("tr-TR")); // 7 March 2026

ToDecimal(string?, decimal, IFormatProvider?)

Converts a string to a decimal.

public static decimal ToDecimal(this string? value, decimal defaultValue = 0, IFormatProvider? provider = null)

Parameters

value string

The string to convert.

defaultValue decimal

The value returned when the string is null, empty or invalid.

provider IFormatProvider

The format provider for strict parsing, or null for the flexible rule.

Returns

decimal

The decimal, or defaultValue.

Examples

"1.234,56".ToDecimal();  // 1234.56
"1234.56".ToDecimal();   // 1234.56
"abc".ToDecimal(-1m);    // -1

Remarks

Without a provider the conversion is flexible: a comma is the decimal separator when present ("1.234,56"), otherwise a dot is ("1234.56"). With a provider the string is parsed strictly with that culture.

ToEnum<T>(string?, T)

Converts a string to an enum value by name (ignoring case) or by a defined numeric value.

public static T ToEnum<T>(this string? value, T defaultValue = default) where T : struct, Enum

Parameters

value string

The string to convert.

defaultValue T

The value returned when the string is null, empty or not a defined value.

Returns

T

The enum value, or defaultValue.

Type Parameters

T

The enum type.

Examples

"Friday".ToEnum<DayOfWeek>();   // DayOfWeek.Friday
"5".ToEnum<DayOfWeek>();        // DayOfWeek.Friday
"99".ToEnum(DayOfWeek.Monday);  // DayOfWeek.Monday

ToInt(string?, int)

Converts a string to an integer.

public static int ToInt(this string? value, int defaultValue = 0)

Parameters

value string

The string to convert.

defaultValue int

The value returned when the string is null, empty or invalid.

Returns

int

The integer, or defaultValue.

ToLong(string?, long)

Converts a string to a long integer.

public static long ToLong(this string? value, long defaultValue = 0)

Parameters

value string

The string to convert.

defaultValue long

The value returned when the string is null, empty or invalid.

Returns

long

The long integer, or defaultValue.

ToNullable<T>(string?, IFormatProvider?)

Converts a string to a nullable value. Returns null when the string is null, empty or invalid.

public static T? ToNullable<T>(this string? value, IFormatProvider? provider = null) where T : struct, IParsable<T>

Parameters

value string

The string to convert.

provider IFormatProvider

The format provider. Defaults to InvariantCulture.

Returns

T?

The converted value, or null.

Type Parameters

T

The target value type.

Examples

"42".ToNullable<int>();   // 42
"abc".ToNullable<int>();  // null

ToShort(string?, short)

Converts a string to a short integer.

public static short ToShort(this string? value, short defaultValue = 0)

Parameters

value string

The string to convert.

defaultValue short

The value returned when the string is null, empty or invalid.

Returns

short

The short integer, or defaultValue.

To<T>(string?, T, IFormatProvider?)

Converts a string to any type that implements IParsable<TSelf>, such as int, decimal, DateTime, DateOnly or Guid.

public static T To<T>(this string? value, T defaultValue = default, IFormatProvider? provider = null) where T : IParsable<T>

Parameters

value string

The string to convert.

defaultValue T

The value returned when the string is null, empty or invalid.

provider IFormatProvider

The format provider. Defaults to InvariantCulture.

Returns

T

The converted value, or defaultValue.

Type Parameters

T

The target type.

Examples

"42".To<int>();                 // 42
"2026-03-07".To<DateOnly>();    // 2026-03-07
"x".To(defaultValue: -1);       // -1

TryToDecimal(string?, out decimal, IFormatProvider?)

Tries to convert a string to a decimal with the same rules as ToDecimal(string?, decimal, IFormatProvider?).

public static bool TryToDecimal(this string? value, out decimal result, IFormatProvider? provider = null)

Parameters

value string

The string to convert.

result decimal

The decimal when the method returns true; otherwise zero.

provider IFormatProvider

The format provider for strict parsing, or null for the flexible rule.

Returns

bool

True if the string was converted; otherwise false.

TryToEnum<T>(string?, out T)

Tries to convert a string to an enum value by name (ignoring case) or by a defined numeric value.

public static bool TryToEnum<T>(this string? value, out T result) where T : struct, Enum

Parameters

value string

The string to convert.

result T

The enum value when the method returns true; otherwise the default value.

Returns

bool

True if the string matches a defined value; otherwise false.

Type Parameters

T

The enum type.

TryTo<T>(string?, out T, IFormatProvider?)

Tries to convert a string to any type that implements IParsable<TSelf>.

public static bool TryTo<T>(this string? value, out T result, IFormatProvider? provider = null) where T : IParsable<T>

Parameters

value string

The string to convert.

result T

The converted value when the method returns true; otherwise the default value of T.

provider IFormatProvider

The format provider. Defaults to InvariantCulture.

Returns

bool

True if the string was converted; otherwise false.

Type Parameters

T

The target type.

Examples

if ("42".TryTo(out int number)) { /* number == 42 */ }