Search Results for

    Show / Hide Table of Contents

    Class StringHelper

    A collection of helper functions for common string manipulation

    Inheritance
    object
    StringHelper
    Inherited Members
    object.GetType()
    object.MemberwiseClone()
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    Namespace: LemonEdge.Utils
    Assembly: LemonEdge.Utils.dll
    Syntax
    public static class StringHelper

    Fields

    DataItemsSeparator

    Used when seperating data items in a single string

    Declaration
    public const string DataItemsSeparator = ";;"
    Field Value
    Type Description
    string

    DateInternationalFormat

    Formats a datetime as a single date in international, sortable format

    Declaration
    public const string DateInternationalFormat = "yyyy-MM-dd"
    Field Value
    Type Description
    string

    DateInternationalPreciseFormat

    Formats a datetime as a date and time in international, sortable format complete with milliseconds

    Declaration
    public const string DateInternationalPreciseFormat = "yyyy-MM-dd HH:mm:ss.fff"
    Field Value
    Type Description
    string

    DateInternationalTimeNoSecondsFormat

    Formats a datetime as a date and time in international, sortable format without the seconds component

    Declaration
    public const string DateInternationalTimeNoSecondsFormat = "yyyy-MM-dd HH:mm"
    Field Value
    Type Description
    string

    DateLongUserFriendlyFormat

    Formats a datetime as a date in the long user friendly format using current culture

    Declaration
    public const string DateLongUserFriendlyFormat = "D"
    Field Value
    Type Description
    string

    DateShortUserFriendlyFormat

    Formats a datetime as a date short user friendly format using current culture

    Declaration
    public const string DateShortUserFriendlyFormat = "d"
    Field Value
    Type Description
    string

    DateTimeFileFriendlyFormat

    Formats a datetime as a date and time in international, sortable format without the seconds component or colon separators

    Declaration
    public const string DateTimeFileFriendlyFormat = "yyyy-MM-dd HHmmss"
    Field Value
    Type Description
    string

    DateTimeInternationalFormat

    Formats a datetime as a date and time in international, sortable format

    Declaration
    public const string DateTimeInternationalFormat = "yyyy-MM-dd HH:mm:ss"
    Field Value
    Type Description
    string

    DateTimeLongUserFriendlyFormat

    Formats a datetime as a date and time in the long user friendly format using current culture

    Declaration
    public const string DateTimeLongUserFriendlyFormat = "F"
    Field Value
    Type Description
    string

    DateTimeOffsetPreciseFormat

    Formats a datetimeoffset as a date and time in international, sortable format with precision

    Declaration
    public const string DateTimeOffsetPreciseFormat = "yyyy-MM-ddTHH:mm:ss.fffffffK"
    Field Value
    Type Description
    string

    DateTimeShortUserFriendlyFormat

    Formats a datetime as a date and time in the short user friendly format using current culture

    Declaration
    public const string DateTimeShortUserFriendlyFormat = "g"
    Field Value
    Type Description
    string

    RecordItemsSeparator

    Used when seperating record items in a single string

    Declaration
    public const string RecordItemsSeparator = ";;;"
    Field Value
    Type Description
    string

    TimeLongUserFriendlyFormat

    Formats a datetime as a in the long user friendly format using current culture

    Declaration
    public const string TimeLongUserFriendlyFormat = "T"
    Field Value
    Type Description
    string

    TimeShortUserFriendlyFormat

    Formats a datetime as a time in the short user friendly format using current culture

    Declaration
    public const string TimeShortUserFriendlyFormat = "t"
    Field Value
    Type Description
    string

    Methods

    FromSafeIISQueryString(string)

    Replaces any instances of '&fs' with '/' for IIS integration with query strings

    Declaration
    public static string FromSafeIISQueryString(this string value)
    Parameters
    Type Name Description
    string value

    A string to be replaced

    Returns
    Type Description
    string

    FromStringValue(Type, string)

    Parses a string value and returns the item as an instance of the specified type t if it can be converted as such

    Declaration
    public static (bool Valid, object Result) FromStringValue(Type t, string value)
    Parameters
    Type Name Description
    Type t

    The type to convert the given value into

    string value

    The string representation of the type t to be converted into

    Returns
    Type Description
    (bool Valid, object Result)

    A Tuple indicating if the conversion was sucessfull and an instance of the type t if it was

    FromStringValue<T>(string)

    Parses a string value and returns the item as an instance of the specified type T if it can be converted as such

    Declaration
    public static (bool Valid, T Result) FromStringValue<T>(string value)
    Parameters
    Type Name Description
    string value

    The string representation of the type T to be converted into

    Returns
    Type Description
    (bool ExistedInCache, T KeyItem)

    A Tuple indicating if the conversion was sucessfull and an instance of the type T if it was

    Type Parameters
    Name Description
    T

    The type to convert the given value into

    GetDefaultStandardFormat(Type)

    Returns the default string formatting to apply to given types

    Declaration
    public static string GetDefaultStandardFormat(Type type)
    Parameters
    Type Name Description
    Type type

    The type to see if there is a default format display for

    Returns
    Type Description
    string

    A default string formatting to apply to instances of the given type

    Remarks

    For instance by default LemonEdge displays decimals to 3 decimal places unless otherwise supplied a specific format

    GetRandomAlpha(CharLetterTypeCase)

    Returns a random alphabet character according to the type parameter

    Declaration
    public static char GetRandomAlpha(StringHelper.CharLetterTypeCase type)
    Parameters
    Type Name Description
    StringHelper.CharLetterTypeCase type

    Indicates if the character returned should come from the set of characters that are lower case, upper case, or both

    Returns
    Type Description
    char

    A single random alphabet character

    GetRandomTextString(int)

    Returns a string of random alphabet characters (of upper and lower case) of the specified length

    Declaration
    public static string GetRandomTextString(int length)
    Parameters
    Type Name Description
    int length

    The length of the string to return

    Returns
    Type Description
    string

    A string of random alphabet characters (of upper and lower case) up to the specified length

    IsSafeFileName(string, bool, bool)

    Ensures that the provided string can be used as a valid filename. In other words only contains valid alphanumeric characters and the underscore.

    Declaration
    public static bool IsSafeFileName(this string s, bool allowNullOrEmpty, bool allowParamAtSign)
    Parameters
    Type Name Description
    string s

    A string to be validated if it only contains akphanumeric characters and the underscore

    bool allowNullOrEmpty

    Indicates if a null or empty string is valid

    bool allowParamAtSign
    Returns
    Type Description
    bool

    True if the string only contains alphanumeric characters and the underscore, false otherwise

    IsSafeFileName(string, bool, IEnumerable<char>)

    Ensures that the provided string can be used as a valid filename. In other words only contains valid alphanumeric characters and specified validNonAlphaNumericalChars characters

    Declaration
    public static bool IsSafeFileName(this string s, bool allowNullOrEmpty, IEnumerable<char> validNonAlphaNumericalChars = null)
    Parameters
    Type Name Description
    string s

    A string to be validated if it only contains akphanumeric characters and specified validNonAlphaNumericalChars characters

    bool allowNullOrEmpty

    Indicates if a null or empty string is valid

    IEnumerable<char> validNonAlphaNumericalChars

    A list of non alphanumerical characters that are also valid to be included in a filename, such as ' ' or '_'

    Returns
    Type Description
    bool

    True if the string only contains alphanumeric characters and specified validNonAlphaNumericalChars characters, false otherwise

    IsURLRelative(string)

    Declaration
    public static bool IsURLRelative(string url)
    Parameters
    Type Name Description
    string url
    Returns
    Type Description
    bool

    MakeCSVSafeString(string, string)

    Returns a csv safe version of this string. If the string contains a csvSeparator then it is returned surrounded in quotes

    Declaration
    public static string MakeCSVSafeString(this string value, string csvSeparator = ",")
    Parameters
    Type Name Description
    string value

    The string to return surounded in quotes if it contains the csvSeparator and is not already surrounded by quotes itself

    string csvSeparator

    The separator for csv items to check for in the value

    Returns
    Type Description
    string

    A csv safe version of this string. If the string contains a csvSeparator then it is returned surrounded in quotes

    OpenBrowser(string)

    A process that takes a valid url and opens it in the default browser across possible .Net Core platforms

    Declaration
    public static void OpenBrowser(string url)
    Parameters
    Type Name Description
    string url

    The url to open in a new browser

    Remarks

    Taken from https://brockallen.com/2016/09/24/process-start-for-urls-on-net-core/

    ToCSV(IEnumerable, string[], Type[], string[])

    Returns a csv formatted StringBuilder of the specified items with columns from the specified properties

    Declaration
    public static StringBuilder ToCSV(this IEnumerable items, string[] properties, Type[] propertyTypes, string[] friendlyExcelHeaders = null)
    Parameters
    Type Name Description
    IEnumerable items

    A collection of items to return as csv formatted rows

    string[] properties

    The properties on the items that should be returned as columns in the csv format

    Type[] propertyTypes

    The types of the properties to be returned as columns in the csv format

    string[] friendlyExcelHeaders

    The properties are always displayed as the column headers. This will be displayed as a second line of headers to be used in the way LemonEdge handles headers in excel, the second line (this one) is displayed whereas the first is tuend into named ranges

    Returns
    Type Description
    StringBuilder

    A comma seperated list with a row for each item in the items collection

    Remarks

    For example the following:

    class MyItem
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
    }
    
    public string DisplayAsCSV()
    {
        var items = new List<MyItem>() { new MyItem() { Prop1 = "Row1", Prop2 = "Value1" }, new MyItem() { Prop1 = "Row2", Prop2 = "Value2" }, };
        return StringHelper.ToCSV(items, new string[] { nameof(MyItem.Prop1) }, new Type[] { typeof(String) }).ToString();
    }

    will return:

    Prop1,Prop2

    Row1,Value1

    Row2,Value2

    ToSafeFileName(string)

    Strips out any characters that would be invalid in a filename. This would be any non alphanumeric characters that are not a space or underscore

    Declaration
    public static string ToSafeFileName(this string s)
    Parameters
    Type Name Description
    string s

    The string to be returned as a safe filename string

    Returns
    Type Description
    string

    An instance of s with all invalid filename characters stripped out

    ToSafeFileName(string, IEnumerable<char>)

    Strips out any characters that would be invalid in a filename. This would be any non alphanumeric characters that are not a space or underscore

    Declaration
    public static string ToSafeFileName(this string s, IEnumerable<char> validNonAlphaNumericalChars = null)
    Parameters
    Type Name Description
    string s

    The string to be returned as a safe filename string

    IEnumerable<char> validNonAlphaNumericalChars

    A list of non alphanumerical characters that are also valid to be included in a file name such as ' ' or '_'

    Returns
    Type Description
    string

    An instance of s with all invalid filename characters stripped out

    ToSafeIISQueryString(string)

    Replaces '/' character with '&fs' for IIS integration with query strings

    Declaration
    public static string ToSafeIISQueryString(this string value)
    Parameters
    Type Name Description
    string value

    A string to be replaced

    Returns
    Type Description
    string

    ToStringValue(Type?, object?)

    Formats the current object using its associated default string representation

    Declaration
    public static string ToStringValue(Type? t, object? value)
    Parameters
    Type Name Description
    Type t

    The type of the value to be represented using a string format

    object value

    The object instance of type T to be represented in a default string format

    Returns
    Type Description
    string

    A default string formatted representation of the given value

    Remarks

    For example the following types will return:

    • Enum Values: Returns string representation of Enum value
    • DateTimeOffsets: Returns string representation in DateTimeOffsetPreciseFormat
    • DateTimes: Returns string representation in DateInternationalPreciseFormat
    • Bytes: Returns hexadecimal string equivalent starting 0x

    ToStringValue<T>(T)

    Formats the current object using its associated default string representation

    Declaration
    public static string ToStringValue<T>(T value)
    Parameters
    Type Name Description
    T value

    The object instance of T to be represented in a default string format

    Returns
    Type Description
    string

    A default string formatted representation of the given value

    Type Parameters
    Name Description
    T

    The type of object to be represented using a string format

    Remarks

    For example the following types will return:

    • Enum Values: Returns string representation of Enum value
    • DateTimeOffsets: Returns string representation in DateTimeOffsetPreciseFormat
    • DateTimes: Returns string representation in DateInternationalPreciseFormat
    • Bytes: Returns hexadecimal string equivalent starting 0x

    TryParseFromString<T>(string, out T)

    Parses a string value and returns true if it is valid. The item is returned as an instance of the specified type T in the out parameter result

    Declaration
    public static bool TryParseFromString<T>(string value, out T result)
    Parameters
    Type Name Description
    string value

    A string value of type T

    T result

    If the string representation value is valid and can be parsed, this returns the actual value

    Returns
    Type Description
    bool

    True if the string representation can be parsed

    Type Parameters
    Name Description
    T

    The type of item to parse the string representation of

    In this article
    Back to top © LemonEdge Technologies. All rights reserved.