Lean  $LEAN_TAG$
QuantConnect.StringExtensions Class Reference

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

Static Public Member Functions

static T ConvertInvariant< T > (this object value)
 Converts the provided value as T using CultureInfo More...
 
static object ConvertInvariant (this object value, Type conversionType)
 Converts the provided value as conversionType using CultureInfo More...
 
static string Invariant (FormattableString formattable)
 Non-extension method alias for FormattableString.Invariant This supports the More...
 
static string ToStringInvariant (this IConvertible convertible)
 Converts the provided value to a string using CultureInfo More...
 
static string ToStringInvariant (this IFormattable formattable, string format)
 Formats the provided value using the specified format and CultureInfo More...
 
static string ToIso8601Invariant (this DateTime dateTime)
 Provides a convenience methods for converting a DateTime to an invariant ISO-8601 string More...
 
static bool StartsWithInvariant (this string value, string beginning, bool ignoreCase=false)
 Checks if the string starts with the provided beginning using CultureInfo while optionally ignoring case. More...
 
static bool EndsWithInvariant (this string value, string ending, bool ignoreCase=false)
 Checks if the string ends with the provided ending using CultureInfo while optionally ignoring case. More...
 
static int IndexOfInvariant (this string value, char character)
 Gets the index of the specified character using StringComparison More...
 
static int IndexOfInvariant (this string value, string substring, bool ignoreCase=false)
 Gets the index of the specified substring using StringComparison or System.StringComparison.InvariantCultureIgnoreCase when ignoreCase is true More...
 
static int LastIndexOfInvariant (this string value, string substring, bool ignoreCase=false)
 Gets the index of the specified substring using StringComparison or System.StringComparison.InvariantCultureIgnoreCase when ignoreCase is true More...
 
static T IfNotNullOrEmpty< T > (this string value, T defaultValue, Func< string, T > func)
 Provides a shorthand for avoiding the more verbose ternary equivalent. Consider the following: More...
 
static T IfNotNullOrEmpty< T > (this string value, Func< string, T > func)
 Provides a shorthand for avoiding the more verbose ternary equivalent. Consider the following: More...
 
static string SafeSubstring (this string value, int startIndex, int length)
 Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length. More...
 
static string Truncate (this string value, int maxLength)
 Truncates a string to the specified maximum length More...
 

Detailed Description

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

Definition at line 26 of file StringExtensions.cs.

Member Function Documentation

◆ ConvertInvariant< T >()

static T QuantConnect.StringExtensions.ConvertInvariant< T > ( this object  value)
static

Converts the provided value as T using CultureInfo

Definition at line 36 of file StringExtensions.cs.

◆ ConvertInvariant()

static object QuantConnect.StringExtensions.ConvertInvariant ( this object  value,
Type  conversionType 
)
static

Converts the provided value as conversionType using CultureInfo

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 IConvertible.ToType 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.

Definition at line 51 of file StringExtensions.cs.

◆ Invariant()

static string QuantConnect.StringExtensions.Invariant ( FormattableString  formattable)
static

Non-extension method alias for FormattableString.Invariant 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 FormattableString.Invariant

Definition at line 127 of file StringExtensions.cs.

◆ ToStringInvariant() [1/2]

static string QuantConnect.StringExtensions.ToStringInvariant ( this IConvertible  convertible)
static

Converts the provided value to a string using CultureInfo

Definition at line 135 of file StringExtensions.cs.

◆ ToStringInvariant() [2/2]

static string QuantConnect.StringExtensions.ToStringInvariant ( this IFormattable  formattable,
string  format 
)
static

Formats the provided value using the specified format and CultureInfo

Definition at line 149 of file StringExtensions.cs.

◆ ToIso8601Invariant()

static string QuantConnect.StringExtensions.ToIso8601Invariant ( this DateTime  dateTime)
static

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

Definition at line 182 of file StringExtensions.cs.

◆ StartsWithInvariant()

static bool QuantConnect.StringExtensions.StartsWithInvariant ( this string  value,
string  beginning,
bool  ignoreCase = false 
)
static

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

Definition at line 191 of file StringExtensions.cs.

◆ EndsWithInvariant()

static bool QuantConnect.StringExtensions.EndsWithInvariant ( this string  value,
string  ending,
bool  ignoreCase = false 
)
static

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

Definition at line 200 of file StringExtensions.cs.

◆ IndexOfInvariant() [1/2]

static int QuantConnect.StringExtensions.IndexOfInvariant ( this string  value,
char  character 
)
static

Gets the index of the specified character using StringComparison

Definition at line 208 of file StringExtensions.cs.

◆ IndexOfInvariant() [2/2]

static int QuantConnect.StringExtensions.IndexOfInvariant ( this string  value,
string  substring,
bool  ignoreCase = false 
)
static

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

Definition at line 217 of file StringExtensions.cs.

◆ LastIndexOfInvariant()

static int QuantConnect.StringExtensions.LastIndexOfInvariant ( this string  value,
string  substring,
bool  ignoreCase = false 
)
static

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

Definition at line 229 of file StringExtensions.cs.

◆ IfNotNullOrEmpty< T >() [1/2]

static T QuantConnect.StringExtensions.IfNotNullOrEmpty< T > ( this string  value,
defaultValue,
Func< string, T >  func 
)
static

Provides a shorthand for avoiding the more verbose ternary equivalent. Consider the following:

string.IsNullOrEmpty(str) ? (decimal?)null : Convert.ToDecimal(str, CultureInfo.InvariantCulture) Can be expressed as: str.IfNotNullOrEmpty<decimal?>(s => Convert.ToDecimal(str, CultureInfo.InvariantCulture)) When combined with additional methods from this class, reducing further to a declarative: str.IfNotNullOrEmpty<decimal?>(s => s.ParseDecimalInvariant()) str.IfNotNullOrEmpty<decimal?>(s => s.ConvertInvariant<decimal>())

value The string value to check for null or empty defaultValue The default value to use if null or empty func Function run on non-null string w/ length > 0

Definition at line 256 of file StringExtensions.cs.

◆ IfNotNullOrEmpty< T >() [2/2]

static T QuantConnect.StringExtensions.IfNotNullOrEmpty< T > ( this string  value,
Func< string, T >  func 
)
static

Provides a shorthand for avoiding the more verbose ternary equivalent. Consider the following:

string.IsNullOrEmpty(str) ? (decimal?)null : Convert.ToDecimal(str, CultureInfo.InvariantCulture) Can be expressed as: str.IfNotNullOrEmpty<decimal?>(s => Convert.ToDecimal(str, CultureInfo.InvariantCulture)) When combined with additional methods from this class, reducing further to a declarative: str.IfNotNullOrEmpty<decimal?>(s => s.ParseDecimalInvariant()) str.IfNotNullOrEmpty<decimal?>(s => s.ConvertInvariant<decimal>())

value The string value to check for null or empty func Function run on non-null string w/ length > 0

Definition at line 284 of file StringExtensions.cs.

◆ SafeSubstring()

static string QuantConnect.StringExtensions.SafeSubstring ( this string  value,
int  startIndex,
int  length 
)
static

Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

startIndex The zero-based starting character position of a substring in this instance length The number of characters in the substring

Definition at line 295 of file StringExtensions.cs.

◆ Truncate()

static string QuantConnect.StringExtensions.Truncate ( this string  value,
int  maxLength 
)
static

Truncates a string to the specified maximum length

Parameters
valueThe string
maxLengthThe maximum allowed string
Returns
A new string with maxLength characters if the original one's length was greater than the maximum allowed length. Otherwise, the original string is returned.

Definition at line 324 of file StringExtensions.cs.


The documentation for this class was generated from the following file: