Search Results for

    Show / Hide Table of Contents

    Class StringExtensions

    A set of extension methods for string manipulation

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

    Methods

    ContainsWholeWord(String, String, StringComparison, String[])

    Indicates if the specified string contains any instances of the specified word but only if it exists as a single word, not contained in other words.

    Declaration
    public static bool ContainsWholeWord(this string a, string word, StringComparison comparrisonType, string[] except = null)
    Parameters
    Type Name Description
    System.String a

    The source string to determine if any whole instances of word exist within it

    System.String word

    The word to search for. Only returns true if the word exists as a whole word, not as part of any other word

    System.StringComparison comparrisonType

    The System.StringComparison type to use when searching for the word

    System.String[] except

    A list of words to exclude from possible matches

    Returns
    Type Description
    System.Boolean

    True if the specified word exists in the string by itself and not as part of any other word

    Remarks

    For example searching for "table" will return false if the string to search is "Only looking for whole words, so tabletop does not count"

    A whole word is defined as a word containing alphanumeric characters and '_' or '-'

    GetEnclosedString(String, String, String, Int32)

    Returns the text found in between the specified suffix and prefix

    Declaration
    public static string GetEnclosedString(this string source, string prefix, string suffix, int startIndex = 0)
    Parameters
    Type Name Description
    System.String source

    The string to search for any text in between the specified suffix and prefix

    System.String prefix

    The string that defines the beginning of a string to return when found in the source string

    System.String suffix

    The string that defines the end of a string to return when found in the source string

    System.Int32 startIndex

    An optional start index to start the search from within the source string. Defaults to 0 the start of the string.

    Returns
    Type Description
    System.String

    The first instance of any string contained within prefix and suffix found on or after startIndex

    Remarks

    For example calling:

    "Get my enclosed values (here they are) from the brackets".GetEnclosedString("(", ")");

    Would return:

    "here they are"

    See Also
    GetEnclosedStrings(String, String, String)

    GetEnclosedStrings(String, String, String)

    Returns all instances of text found between the specified suffix and prefix

    Declaration
    public static IEnumerable<string> GetEnclosedStrings(this string source, string prefix, string suffix)
    Parameters
    Type Name Description
    System.String source

    The string to search for any text in between the specified suffix and prefix

    System.String prefix

    The string that defines the beginning of a string to return when found in the source string

    System.String suffix

    The string that defines the end of a string to return when found in the source string

    Returns
    Type Description
    System.Collections.Generic.IEnumerable<System.String>

    All instances of any string contained within prefix and suffix

    Remarks

    For example calling:

    "Get my enclosed values (here they are) from (and here) the brackets".GetEnclosedString("(", ")");

    Would return:

    "here they are", "and here"

    See Also
    GetEnclosedString(String, String, String, Int32)

    Pluralize(String)

    Returns a plural version of a singular word. For instance changing Changer to Changers

    Declaration
    public static string Pluralize(this string s)
    Parameters
    Type Name Description
    System.String s

    The name to turn into a plural version

    Returns
    Type Description
    System.String

    A plural version of the original s string

    Remarks

    For example:

    • Strategy: Returns Strategies
    • Class: Returns Classes
    • Desk: Returns Desks
    See Also
    Singularize(String)

    ReplaceWholeWord(String, String, String, StringComparison)

    Replaces instances of the word string with the replace value, but only when it is found as a whole word and not as part of any other word

    Declaration
    public static string ReplaceWholeWord(this string a, string word, string replace, StringComparison comparrisonType)
    Parameters
    Type Name Description
    System.String a

    The source string to search and replace a word in

    System.String word

    The whole word to search for and replace all instances with replace

    System.String replace

    The string to use to replace all found whole word instances of word with

    System.StringComparison comparrisonType

    The System.StringComparison type to use when searching for the whole word

    Returns
    Type Description
    System.String

    A new string with all whole word instances of word replaced with the specified replace string

    Remarks

    For example:

    return "searching for search to replace with newword".ReplaceWholeWord("search", "newword", StringComparison.InvariantCultureIgnoreCase);

    Would return

    "searching for newword to replace with newword"

    See Also
    ReplaceWholeWords(String, String[], String[], StringComparison)

    ReplaceWholeWords(String, String[], String[], StringComparison)

    Replaces instances of the words string with the associated replaces value, but only when it is found as a whole word and not as part of any other word

    Declaration
    public static string ReplaceWholeWords(this string a, string[] words, string[] replaces, StringComparison comparrisonType)
    Parameters
    Type Name Description
    System.String a

    The source string to search and replace a word in

    System.String[] words

    A list of whole words to be replacedreplace

    System.String[] replaces

    A list of associated strings to replace all found whole word instances of. This should have the same length as the words argumentword with

    System.StringComparison comparrisonType

    The System.StringComparison type to use when searching for the matching words

    Returns
    Type Description
    System.String

    A new string with all whole word instances of words replaced with the specified replaces string

    See Also
    ReplaceWholeWord(String, String, String, StringComparison)

    Singularize(String)

    Returns a singular version of a plural word. For instance changing Changers to Changer

    Declaration
    public static string Singularize(this string s)
    Parameters
    Type Name Description
    System.String s

    The name to turn into a singular version

    Returns
    Type Description
    System.String

    A singular version of the original s string

    Remarks

    For example:

    • Strategies: Returns Strategy
    • Classes: Returns Class
    • Desks: Returns Desk
    • Singular: Returns Singular
    See Also
    Pluralize(String)

    ToCommaSeperatedList(IEnumerable)

    Returns a comma separated collection of items as a System.Text.StringBuilder

    Declaration
    public static StringBuilder ToCommaSeperatedList(this IEnumerable items)
    Parameters
    Type Name Description
    System.Collections.IEnumerable items

    A list of items to enumerate and format in the returned string

    Returns
    Type Description
    System.Text.StringBuilder

    A comma separated System.Text.StringBuilder holding each item in the items enumeration

    Remarks

    For example:

    (new string[] { "One", "Two", "Three" }).ToCommaSeperatedList().ToString();

    Would return:

    "One,Two,Three"

    See Also
    ToCommaSeperatedList(IEnumerable, Func<Object, String>, String, String)
    ToCommaSeperatedList<T>(IEnumerable<T>, Func<T, String>, String, String)
    ToCommaSeperatedList(IEnumerable, String, String)
    ToConcatenatedList(IEnumerable, Func<Object, String>, String, String, String)
    ToConcatenatedList<T>(IEnumerable<T>, Func<T, String>, String, String, String)
    ToConcatenatedList(IEnumerable, String, String, String)
    ToConcatenatedList(IEnumerable, String)

    ToCommaSeperatedList(IEnumerable, Func<Object, String>, String, String)

    Returns a comma separated collection of items as a System.Text.StringBuilder with an optional prefix and suffix too

    Declaration
    public static StringBuilder ToCommaSeperatedList(this IEnumerable items, Func<object, string> value, string prefix, string suffix)
    Parameters
    Type Name Description
    System.Collections.IEnumerable items

    A list of items to enumerate and format in the returned string

    System.Func<System.Object, System.String> value

    A function that provides a string representation of an instance of an object in the items enumeration

    System.String prefix

    A string to create before the string representation of each item in the items enumeration

    System.String suffix

    A string to create after the srting representation of each item in the items enumeration

    Returns
    Type Description
    System.Text.StringBuilder

    A comma separated System.Text.StringBuilder holding each item in the items enumeration with specified prefix and suffix characters

    Remarks

    For example:

    (new string[] { "One", "Two", "Three" }).ToCommaSeperatedList(x => x, "'", "'").ToString();

    Would return:

    "'One','Two','Three'"

    See Also
    ToCommaSeperatedList<T>(IEnumerable<T>, Func<T, String>, String, String)
    ToCommaSeperatedList(IEnumerable, String, String)
    ToCommaSeperatedList(IEnumerable)
    ToConcatenatedList(IEnumerable, Func<Object, String>, String, String, String)
    ToConcatenatedList<T>(IEnumerable<T>, Func<T, String>, String, String, String)
    ToConcatenatedList(IEnumerable, String, String, String)
    ToConcatenatedList(IEnumerable, String)

    ToCommaSeperatedList(IEnumerable, String, String)

    Returns a comma separated collection of items as a System.Text.StringBuilder with an optional prefix and suffix too

    Declaration
    public static StringBuilder ToCommaSeperatedList(this IEnumerable items, string prefix, string suffix)
    Parameters
    Type Name Description
    System.Collections.IEnumerable items

    A list of items to enumerate and format in the returned string

    System.String prefix

    A string to create before the string representation of each item in the items enumeration

    System.String suffix

    A string to create after the srting representation of each item in the items enumeration

    Returns
    Type Description
    System.Text.StringBuilder

    A comma separated System.Text.StringBuilder holding each item in the items enumeration with specified prefix and suffix characters

    Remarks

    For example:

    (new string[] { "One", "Two", "Three" }).ToCommaSeperatedList("'", "'").ToString();

    Would return:

    "'One','Two','Three'"

    See Also
    ToCommaSeperatedList(IEnumerable, Func<Object, String>, String, String)
    ToCommaSeperatedList<T>(IEnumerable<T>, Func<T, String>, String, String)
    ToCommaSeperatedList(IEnumerable)
    ToConcatenatedList(IEnumerable, Func<Object, String>, String, String, String)
    ToConcatenatedList<T>(IEnumerable<T>, Func<T, String>, String, String, String)
    ToConcatenatedList(IEnumerable, String, String, String)
    ToConcatenatedList(IEnumerable, String)

    ToCommaSeperatedList<T>(IEnumerable<T>, Func<T, String>, String, String)

    Returns a comma separated collection of items as a System.Text.StringBuilder with an optional prefix and suffix too

    Declaration
    public static StringBuilder ToCommaSeperatedList<T>(this IEnumerable<T> items, Func<T, string> value, string prefix, string suffix)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<T> items

    A list of items to enumerate and format in the returned string

    System.Func<T, System.String> value

    A function that provides a string representation of an instance of T in the items enumeration

    System.String prefix

    A string to create before the string representation of each item in the items enumeration

    System.String suffix

    A string to create after the srting representation of each item in the items enumeration

    Returns
    Type Description
    System.Text.StringBuilder

    A comma separated System.Text.StringBuilder holding each item in the items enumeration with specified prefix and suffix characters

    Type Parameters
    Name Description
    T

    The type of object in the items enumeration

    Remarks

    For example:

    (new string[] { "One", "Two", "Three" }).ToCommaSeperatedList<string>(x => x, "'", "'").ToString();

    Would return:

    "'One','Two','Three'"

    See Also
    ToCommaSeperatedList(IEnumerable, Func<Object, String>, String, String)
    ToCommaSeperatedList(IEnumerable, String, String)
    ToCommaSeperatedList(IEnumerable)
    ToConcatenatedList(IEnumerable, Func<Object, String>, String, String, String)
    ToConcatenatedList<T>(IEnumerable<T>, Func<T, String>, String, String, String)
    ToConcatenatedList(IEnumerable, String, String, String)
    ToConcatenatedList(IEnumerable, String)

    ToConcatenatedList(IEnumerable, Func<Object, String>, String, String, String)

    Returns a collection of items as a System.Text.StringBuilder with a specified seperator between each item, and an optional prefix and suffix too

    Declaration
    public static StringBuilder ToConcatenatedList(this IEnumerable items, Func<object, string> toString, string seperator, string prefix, string suffix)
    Parameters
    Type Name Description
    System.Collections.IEnumerable items

    A list of items to enumerate and format in the returned string

    System.Func<System.Object, System.String> toString

    A function that provides a string representation of an instance of an object in the items enumeration

    System.String seperator

    A string to use to separate each item in the items enumeration

    System.String prefix

    A string to create before the string representation of each item in the items enumeration

    System.String suffix

    A string to create after the srting representation of each item in the items enumeration

    Returns
    Type Description
    System.Text.StringBuilder

    A System.Text.StringBuilder holding each item in the items enumeration separated by a specified seperator with prefix and suffix characters

    Remarks

    For example:

    (new string[] { "One", "Two", "Three" }).ToConcatenatedList(x => x, ", ", "'", "'").ToString();

    Would return:

    "'One','Two','Three'"

    See Also
    ToConcatenatedList<T>(IEnumerable<T>, Func<T, String>, String, String, String)
    ToConcatenatedList(IEnumerable, String, String, String)
    ToConcatenatedList(IEnumerable, String)
    ToCommaSeperatedList(IEnumerable, Func<Object, String>, String, String)
    ToCommaSeperatedList<T>(IEnumerable<T>, Func<T, String>, String, String)
    ToCommaSeperatedList(IEnumerable, String, String)
    ToCommaSeperatedList(IEnumerable)

    ToConcatenatedList(IEnumerable, String)

    Returns a collection of items as a System.Text.StringBuilder with a specified seperator between each item

    Declaration
    public static StringBuilder ToConcatenatedList(this IEnumerable items, string seperator)
    Parameters
    Type Name Description
    System.Collections.IEnumerable items

    A list of items to enumerate and format in the returned string

    System.String seperator

    A string to use to separate each item in the items enumeration

    Returns
    Type Description
    System.Text.StringBuilder

    A System.Text.StringBuilder holding each item in the items enumeration separated by a specified seperator

    Remarks

    For example:

    (new string[] { "One", "Two", "Three" }).ToConcatenatedList(", ").ToString();

    Would return:

    "One,Two,Three"

    See Also
    ToConcatenatedList(IEnumerable, Func<Object, String>, String, String, String)
    ToConcatenatedList<T>(IEnumerable<T>, Func<T, String>, String, String, String)
    ToConcatenatedList(IEnumerable, String, String, String)
    ToCommaSeperatedList(IEnumerable, Func<Object, String>, String, String)
    ToCommaSeperatedList<T>(IEnumerable<T>, Func<T, String>, String, String)
    ToCommaSeperatedList(IEnumerable, String, String)
    ToCommaSeperatedList(IEnumerable)

    ToConcatenatedList(IEnumerable, String, String, String)

    Returns a collection of items as a System.Text.StringBuilder with a specified seperator between each item, and an optional prefix and suffix too

    Declaration
    public static StringBuilder ToConcatenatedList(this IEnumerable items, string seperator, string prefix, string suffix)
    Parameters
    Type Name Description
    System.Collections.IEnumerable items

    A list of items to enumerate and format in the returned string

    System.String seperator

    A string to use to separate each item in the items enumeration

    System.String prefix

    A string to create before the string representation of each item in the items enumeration

    System.String suffix

    A string to create after the srting representation of each item in the items enumeration

    Returns
    Type Description
    System.Text.StringBuilder

    A System.Text.StringBuilder holding each item in the items enumeration separated by a specified seperator with prefix and suffix characters

    Remarks

    For example:

    (new string[] { "One", "Two", "Three" }).ToConcatenatedList(", ", "'", "'").ToString();

    Would return:

    "'One','Two','Three'"

    See Also
    ToConcatenatedList(IEnumerable, Func<Object, String>, String, String, String)
    ToConcatenatedList<T>(IEnumerable<T>, Func<T, String>, String, String, String)
    ToConcatenatedList(IEnumerable, String)
    ToCommaSeperatedList(IEnumerable, Func<Object, String>, String, String)
    ToCommaSeperatedList<T>(IEnumerable<T>, Func<T, String>, String, String)
    ToCommaSeperatedList(IEnumerable, String, String)
    ToCommaSeperatedList(IEnumerable)

    ToConcatenatedList<T>(IEnumerable<T>, Func<T, String>, String, String, String)

    Returns a collection of items as a System.Text.StringBuilder with a specified seperator between each item, and an optional prefix and suffix too

    Declaration
    public static StringBuilder ToConcatenatedList<T>(this IEnumerable<T> items, Func<T, string> toString, string seperator, string prefix, string suffix)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<T> items

    A list of items to enumerate and format in the returned string

    System.Func<T, System.String> toString

    A function that provides a string representation of an instance of T in the items enumeration

    System.String seperator

    A string to use to separate each item in the items enumeration

    System.String prefix

    A string to create before the string representation of each item in the items enumeration

    System.String suffix

    A string to create after the srting representation of each item in the items enumeration

    Returns
    Type Description
    System.Text.StringBuilder

    A System.Text.StringBuilder holding each item in the items enumeration separated by a specified seperator with prefix and suffix characters

    Type Parameters
    Name Description
    T

    The type of object in the items enumeration

    Remarks

    For example:

    (new string[] { "One", "Two", "Three" }).ToConcatenatedList<string>(x => x, ", ", "'", "'").ToString();

    Would return:

    "'One','Two','Three'"

    See Also
    ToConcatenatedList(IEnumerable, Func<Object, String>, String, String, String)
    ToConcatenatedList(IEnumerable, String, String, String)
    ToConcatenatedList(IEnumerable, String)
    ToCommaSeperatedList(IEnumerable, Func<Object, String>, String, String)
    ToCommaSeperatedList<T>(IEnumerable<T>, Func<T, String>, String, String)
    ToCommaSeperatedList(IEnumerable, String, String)
    ToCommaSeperatedList(IEnumerable)

    ToCSVFormatString(Object, Type)

    Returns the specified item as a string value that is valid for inclusion in a csv file. For instance the string 'aa,aa' would be returned as '"aa,aa"'

    Declaration
    public static string ToCSVFormatString(this object val, Type type = null)
    Parameters
    Type Name Description
    System.Object val

    The value to be formatted as a csv safe string

    System.Type type

    An optional the value should be evaluated as

    Returns
    Type Description
    System.String

    A standard string representation of the object in a format safe for inclusion in a csv file

    ToField(String)

    Returns the given variable name as a field by prefixing an '_' sign and making the first character lowercase. For instance turning Variable into _variable

    Declaration
    public static string ToField(this string s)
    Parameters
    Type Name Description
    System.String s

    The string to return as a variable field equivelant

    Returns
    Type Description
    System.String

    The provided s string as a variable field name

    See Also
    ToParameter(String)

    ToParameter(String)

    Returns the given variable as a sql parameter by prefixing an '@' sign and making the first character lowercase. For instance turning Variable into @variable

    Declaration
    public static string ToParameter(this string s)
    Parameters
    Type Name Description
    System.String s

    The string to return as a variable equivelant

    Returns
    Type Description
    System.String

    The provided s string as a variable name

    See Also
    ToField(String)

    ToStringGrid<T>(IEnumerable<T>, ITreeNode<(String Name, String Key)>[], (String Name, String GroupKey)[], Func<T, String, String>, Char)

    Returns a string formatted table with rows for each item in items and specified columns and heirarchical headers

    Declaration
    public static string ToStringGrid<T>(this IEnumerable<T> items, ITreeNode<(string Name, string Key)>[] columnGroups, (string Name, string GroupKey)[] columns, Func<T, string, string> getColumnValue, char seperator = '|')
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<T> items

    The collection of items to display as rows in a table

    ITreeNode<System.ValueTuple<System.String, System.String>>[] columnGroups

    Column headers can have heiarchical headers above them. This holds a list of column group headers. columns can specify a group they belong to

    System.ValueTuple<System.String, System.String>[] columns

    A list of columns to display in the table

    System.Func<T, System.String, System.String> getColumnValue

    A function that given an item of type T and a column will return the string representation of that items column value

    System.Char seperator

    An optional column separator. By default is |

    Returns
    Type Description
    System.String

    A formatted string that holds a table with the specified columns and associated group headers. Each item in the items is formatted as a row in the table

    Type Parameters
    Name Description
    T

    The type of item in the items enumeration

    Remarks

    For example:

    public class MyItem
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
    }
    
    public string FormatAsTable()
    {
        var group = new TreeNode<(string, string)>(("Group Header", "GRP"));
        var items = new List<MyItem>() { new MyItem() { Col1 = "Item1.1", Col2 = "Item1.2" }, new MyItem() { Col1 = "Item2.1", Col2 = "Item2.2" } };
        return items.ToStringGrid(
            new ITreeNode<(string Name, string Key)>[] { group },
            new (string, string)[] { (nameof(MyItem.Col1), "GRP"), (nameof(MyItem.Col2), "GRP") },
            (item, col) => col switch { nameof(MyItem.Col1) => item.Col1, nameof(MyItem.Col2) => item.Col2, _ => "" });
    }

    Will return:

    "

    Group Header

    Col1 | Col2

    Item1.1 | Item1.2

    Item2.1 | Item2.2

    "

    The table columns are automatically expanded to a size that makes sure all column values fit

    See Also
    ToStringGrid<T>(IEnumerable<T>, String[], Func<T, String, String>, Char)

    ToStringGrid<T>(IEnumerable<T>, String[], Func<T, String, String>, Char)

    Returns a string formatted table with rows for each item in items and specified columns and heirarchical headers

    Declaration
    public static string ToStringGrid<T>(this IEnumerable<T> items, string[] columns, Func<T, string, string> getColumnValue, char seperator = '|')
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<T> items

    The collection of items to display as rows in a table

    System.String[] columns

    A list of columns to display in the table

    System.Func<T, System.String, System.String> getColumnValue

    A function that given an item of type T and a column will return the string representation of that items column value

    System.Char seperator

    An optional column separator. By default is |

    Returns
    Type Description
    System.String

    A formatted string that holds a table with the specified columns and associated group headers. Each item in the items is formatted as a row in the table

    Type Parameters
    Name Description
    T

    The type of item in the items enumeration

    Remarks

    For example:

    public class MyItem
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
    }
    
    public string FormatAsTable()
    {
        var items = new List<MyItem>() { new MyItem() { Col1 = "Item1.1", Col2 = "Item1.2" }, new MyItem() { Col1 = "Item2.1", Col2 = "Item2.2" } };
        return items.ToStringGrid(
        new string[] { nameof(MyItem.Col1), nameof(MyItem.Col2) },
        (item, col) => col switch { nameof(MyItem.Col1) => item.Col1, nameof(MyItem.Col2) => item.Col2, _ => "" });
    }

    Will return:

    "

    Col1 | Col2

    Item1.1 | Item1.2

    Item2.1 | Item2.2

    "

    The table columns are automatically expanded to a size that makes sure all column values fit

    Wordify(String)

    Takes a given string, often a class name, and returns it as a set of words. For every capital letter in the specified s string the system will create a new word (unless it is an acroynm)

    Declaration
    public static string Wordify(this string s)
    Parameters
    Type Name Description
    System.String s

    The string with a name to return as a set of capitalized words

    Returns
    Type Description
    System.String

    Returns a wordified version of the string, creating a new word for each capitalized part of the original word

    Remarks

    For example:

    • MyClassName: Returns My Class Name
    • TLAClassName: Returns TLA Class Name
    • Singular: Returns Singular
    In This Article
    Back to top © LemonTree Software Ltd. All rights reserved.