Table of Contents

Class StringExtensions

Namespace
FinanceSharp
Assembly
FinanceSharp.dll

Provides extension methods for properly parsing and serializing values while properly using an IFormatProvider/CultureInfo when applicable

public static class StringExtensions
Inheritance
StringExtensions
Inherited Members

Methods

ConvertInvariant(object, Type)

Converts the provided value as conversionType using FinanceSharp.StringExtensions.CultureInfo

public static object ConvertInvariant(this object value, Type conversionType)

Parameters

value object
conversionType Type

Returns

object

Remarks

This implementation uses the Convert.ToXXX methods. This causes null values to be converted to the default value for the provided conversionType. This is in contrast to directly calling ToType(Type, IFormatProvider) which results in an InvalidCastException or a FormatException. Since existing code is dependent on this null -> default value conversion behavior, it has been preserved in this method.

ConvertInvariant<T>(object)

Converts the provided value as T using FinanceSharp.StringExtensions.CultureInfo

public static T ConvertInvariant<T>(this object value)

Parameters

value object

Returns

T

Type Parameters

T

EndsWithInvariant(string, string, bool)

Checks if the string ends with the provided ending using FinanceSharp.StringExtensions.CultureInfo while optionally ignoring case.

public static bool EndsWithInvariant(this string value, string ending, bool ignoreCase = false)

Parameters

value string
ending string
ignoreCase bool

Returns

bool

IfNotNullOrEmpty<T>(string, Func<string, T>)

 Provides a shorthand for avoiding the more verbose ternary equivalent.
 Consider the following:
string.IsNullOrEmpty(str) ? (double?)null : Convert.ToDouble(str, CultureInfo.InvariantCulture)
 Can be expressed as:
str.IfNotNullOrEmpty<double?>(s => Convert.ToDouble(str, CultureInfo.InvariantCulture))
 When combined with additional methods from this class, reducing further to a declarative:
str.IfNotNullOrEmpty<double?>(s => s.ParseDecimalInvariant())
str.IfNotNullOrEmpty<double?>(s => s.ConvertInvariant<double>())
public static T IfNotNullOrEmpty<T>(this string value, Func<string, T> func)

Parameters

value string
func Func<string, T>

Returns

T

Type Parameters

T

IfNotNullOrEmpty<T>(string, T, Func<string, T>)

 Provides a shorthand for avoiding the more verbose ternary equivalent.
 Consider the following:
string.IsNullOrEmpty(str) ? (double?)null : Convert.ToDouble(str, CultureInfo.InvariantCulture)
 Can be expressed as:
str.IfNotNullOrEmpty<double?>(s => Convert.ToDouble(str, CultureInfo.InvariantCulture))
 When combined with additional methods from this class, reducing further to a declarative:
str.IfNotNullOrEmpty<double?>(s => s.ParseDecimalInvariant())
str.IfNotNullOrEmpty<double?>(s => s.ConvertInvariant<double>())
public static T IfNotNullOrEmpty<T>(this string value, T defaultValue, Func<string, T> func)

Parameters

value string
defaultValue T
func Func<string, T>

Returns

T

Type Parameters

T

IndexOfInvariant(string, char)

Gets the index of the specified character using FinanceSharp.StringExtensions.StringComparison

public static int IndexOfInvariant(this string value, char character)

Parameters

value string
character char

Returns

int

IndexOfInvariant(string, string, bool)

Gets the index of the specified substring using FinanceSharp.StringExtensions.StringComparison or InvariantCultureIgnoreCase when ignoreCase is true

public static int IndexOfInvariant(this string value, string substring, bool ignoreCase = false)

Parameters

value string
substring string
ignoreCase bool

Returns

int

Invariant(FormattableString)

	 Non-extension method alias for <xref href="System.FormattableString.Invariant(System.FormattableString)" data-throw-if-not-resolved="false"></xref>
	 This supports the 
using static QuantConnect.StringExtensions

syntax and is aimed at ensuring all formatting is piped through this class instead of alternatively piping through directly to Invariant(FormattableString)

public static string Invariant(FormattableString formattable)

Parameters

formattable FormattableString

Returns

string

LastIndexOfInvariant(string, string, bool)

Gets the index of the specified substring using FinanceSharp.StringExtensions.StringComparison or InvariantCultureIgnoreCase when ignoreCase is true

public static int LastIndexOfInvariant(this string value, string substring, bool ignoreCase = false)

Parameters

value string
substring string
ignoreCase bool

Returns

int

StartsWithInvariant(string, string, bool)

Checks if the string starts with the provided beginning using FinanceSharp.StringExtensions.CultureInfo while optionally ignoring case.

public static bool StartsWithInvariant(this string value, string beginning, bool ignoreCase = false)

Parameters

value string
beginning string
ignoreCase bool

Returns

bool

ToIso8601Invariant(DateTime)

Provides a convenience methods for converting a DateTime to an invariant ISO-8601 string

public static string ToIso8601Invariant(this DateTime dateTime)

Parameters

dateTime DateTime

Returns

string

ToStringInvariant(IConvertible)

Converts the provided value to a string using FinanceSharp.StringExtensions.CultureInfo

public static string ToStringInvariant(this IConvertible convertible)

Parameters

convertible IConvertible

Returns

string

ToStringInvariant(IFormattable, string)

 Formats the provided value using the specified <code class="paramref">format</code> and

FinanceSharp.StringExtensions.CultureInfo

public static string ToStringInvariant(this IFormattable formattable, string format)

Parameters

formattable IFormattable
format string

Returns

string