Class StringExtensions
A set of extension methods for string manipulation
Inherited Members
Namespace: LemonEdge.Utils
Assembly: LemonEdge.Utils.dll
Syntax
public static class StringExtensions
Methods
Contains(StringBuilder, string, bool, bool)
Declaration
public static bool Contains(this StringBuilder sb, string value, bool ignoreCase, bool ignoreSpaces)
Parameters
Type | Name | Description |
---|---|---|
StringBuilder | sb | |
string | value | |
bool | ignoreCase | |
bool | ignoreSpaces |
Returns
Type | Description |
---|---|
bool |
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 |
---|---|---|
string | a | The source string to determine if any whole instances of |
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 |
StringComparison | comparrisonType | The StringComparison type to use when searching for the
|
string[] | except | A list of words to exclude from possible matches |
Returns
Type | Description |
---|---|
bool | True if the specified |
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 '-'
EndWith(string, string)
If the given string doesn't have the given ending, it is appended.
Declaration
public static string EndWith(this string me, string ending)
Parameters
Type | Name | Description |
---|---|---|
string | me | The string value. |
string | ending | The requisite ending. |
Returns
Type | Description |
---|---|
string | The string with the requisite ending. |
GetEnclosedString(string, string, string, int)
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 |
---|---|---|
string | source | The string to search for any text in between the specified |
string | prefix | The string that defines the beginning of a string to return when found in the
|
string | suffix | The string that defines the end of a string to return when found in the |
int | startIndex | An optional start index to start the search from within the |
Returns
Type | Description |
---|---|
string | The first instance of any string contained within |
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)
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 |
---|---|---|
string | source | The string to search for any text in between the specified |
string | prefix | The string that defines the beginning of a string to return when found in the
|
string | suffix | The string that defines the end of a string to return when found in the |
Returns
Type | Description |
---|---|
IEnumerable<string> | All instances of any string contained within |
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
IsNullOrWhiteSpace(string)
Equivalent to calling IsNullOrWhiteSpace(string).
Declaration
public static bool IsNullOrWhiteSpace(this string me)
Parameters
Type | Name | Description |
---|---|---|
string | me | The string. |
Returns
Type | Description |
---|---|
bool | Whether the sting is null, or just whitespace. |
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 |
---|---|---|
string | s | The name to turn into a plural version |
Returns
Type | Description |
---|---|
string | A plural version of the original |
Remarks
For example:
- Strategy: Returns Strategies
- Class: Returns Classes
- Desk: Returns Desks
See Also
RemoveWhiteSpace(string)
Remove whitespace from a string, using IsWhiteSpace(char).
Declaration
public static string RemoveWhiteSpace(this string input)
Parameters
Type | Name | Description |
---|---|---|
string | input | The string to remove with spaces from |
Returns
Type | Description |
---|---|
string |
Replace(StringBuilder, string, string, bool, bool)
Declaration
public static StringBuilder Replace(this StringBuilder a, string match, string replace, bool ignoreCase, bool ignoreSpaces)
Parameters
Type | Name | Description |
---|---|---|
StringBuilder | a | |
string | match | |
string | replace | |
bool | ignoreCase | |
bool | ignoreSpaces |
Returns
Type | Description |
---|---|
StringBuilder |
ReplaceIgnoringWhitespace(StringBuilder, string, string)
Declaration
public static StringBuilder ReplaceIgnoringWhitespace(this StringBuilder input, string find, string replace)
Parameters
Type | Name | Description |
---|---|---|
StringBuilder | input | |
string | find | |
string | replace |
Returns
Type | Description |
---|---|
StringBuilder |
ReplaceStringIgnoringWhitespace(string, string, string)
Declaration
public static string ReplaceStringIgnoringWhitespace(this string input, string find, string replace)
Parameters
Type | Name | Description |
---|---|---|
string | input | |
string | find | |
string | replace |
Returns
Type | Description |
---|---|
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 |
---|---|---|
string | a | The source string to search and replace a word in |
string | word | The whole word to search for and replace all instances with |
string | replace | The string to use to replace all found whole word instances of |
StringComparison | comparrisonType | The StringComparison type to use when searching for the whole
|
Returns
Type | Description |
---|---|
string | A new string with all whole word instances of |
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
ReplaceWholeWord(StringBuilder, string, string, bool)
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 StringBuilder ReplaceWholeWord(this StringBuilder a, string word, string replace, bool ignoreCase)
Parameters
Type | Name | Description |
---|---|---|
StringBuilder | a | The source string to search and replace a word in |
string | word | The whole word to search for and replace all instances with |
string | replace | The string to use to replace all found whole word instances of |
bool | ignoreCase |
Returns
Type | Description |
---|---|
StringBuilder | A new string with all whole word instances of |
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)
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 |
---|---|---|
string | a | The source string to search and replace a word in |
string[] | words | A list of whole words to be replaced |
string[] | replaces | A list of associated strings to replace all found whole word instances of. This should have the
same length as the |
StringComparison | comparrisonType | The StringComparison type to use when searching for the matching
|
Returns
Type | Description |
---|---|
string | A new string with all whole word instances of |
See Also
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 |
---|---|---|
string | s | The name to turn into a singular version |
Returns
Type | Description |
---|---|
string | A singular version of the original |
Remarks
For example:
- Strategies: Returns Strategy
- Classes: Returns Class
- Desks: Returns Desk
- Singular: Returns Singular
See Also
SplitToDictionary(string, char, char)
Splits a string into a dictionary, given an entry and key-value separator.
Declaration
public static Dictionary<string, string> SplitToDictionary(this string me, char entrySeparator = ',', char keyValueSeparator = '=')
Parameters
Type | Name | Description |
---|---|---|
string | me | The text value. |
char | entrySeparator | The entry separator char array. |
char | keyValueSeparator | The key-value separator char array. |
Returns
Type | Description |
---|---|
Dictionary<string, string> | A dictionary of key-value pairs. |
SplitToDictionary(string, char[], char[])
Splits a string into a dictionary, given an entry and key-value separator.
Declaration
public static Dictionary<string, string> SplitToDictionary(this string me, char[] entrySeparator, char[] keyValueSeparator)
Parameters
Type | Name | Description |
---|---|---|
string | me | The text value. |
char[] | entrySeparator | The entry separator char array. |
char[] | keyValueSeparator | The key-value separator char array. |
Returns
Type | Description |
---|---|
Dictionary<string, string> | A dictionary of key-value pairs. |
SplitToGuidArray(string?, char)
Splits a string into a guid array, by the provided separator:
- Splits by separator
- Calls Guid.Parse on each entry (Removes empty entries)
- Returns as an array.
Declaration
public static Guid[] SplitToGuidArray(this string? me, char separator)
Parameters
Type | Name | Description |
---|---|---|
string | me | The string to parse. |
char | separator | The separator. |
Returns
Type | Description |
---|---|
Guid[] | A Guid[] |
StartWith(string, string)
If the given string doesn't have the given beginning, it is prepended.
Declaration
public static string StartWith(this string me, string beginning)
Parameters
Type | Name | Description |
---|---|---|
string | me | The string value. |
string | beginning | The requisite beginning. |
Returns
Type | Description |
---|---|
string | The string with the requisite beginning. |
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 |
---|---|---|
object | val | The value to be formatted as a csv safe string |
Type | type | An optional the value should be evaluated as |
Returns
Type | Description |
---|---|
string | A standard string representation of the object in a format safe for inclusion in a csv file |
ToCommaSeperatedList(IEnumerable)
Returns a comma separated collection of items as a StringBuilder
Declaration
public static StringBuilder ToCommaSeperatedList(this IEnumerable items)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable | items | A list of items to enumerate and format in the returned string |
Returns
Type | Description |
---|---|
StringBuilder | A comma separated StringBuilder holding each item in the |
Remarks
For example:
(new string[] { "One", "Two", "Three" }).ToCommaSeperatedList().ToString();
Would return:
"One,Two,Three"
See Also
ToCommaSeperatedList(IEnumerable, Func<object, string>, string, string)
Returns a comma separated collection of items as a 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 |
---|---|---|
IEnumerable | items | A list of items to enumerate and format in the returned string |
Func<object, string> | value | A function that provides a string representation of an instance of an object in the
|
string | prefix | A string to create before the string representation of each item in the |
string | suffix | A string to create after the srting representation of each item in the |
Returns
Type | Description |
---|---|
StringBuilder | A comma separated StringBuilder holding each item in the |
Remarks
For example:
(new string[] { "One", "Two", "Three" }).ToCommaSeperatedList(x => x, "'", "'").ToString();
Would return:
"'One','Two','Three'"
See Also
ToCommaSeperatedList(IEnumerable, string, string)
Returns a comma separated collection of items as a StringBuilder with an optional
prefix
and suffix
too
Declaration
public static StringBuilder ToCommaSeperatedList(this IEnumerable items, string prefix, string suffix)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable | items | A list of items to enumerate and format in the returned string |
string | prefix | A string to create before the string representation of each item in the |
string | suffix | A string to create after the srting representation of each item in the |
Returns
Type | Description |
---|---|
StringBuilder | A comma separated StringBuilder holding each item in the |
Remarks
For example:
(new string[] { "One", "Two", "Three" }).ToCommaSeperatedList("'", "'").ToString();
Would return:
"'One','Two','Three'"
See Also
ToCommaSeperatedList<T>(IEnumerable<T>, Func<T, string>, string, string)
Returns a comma separated collection of items as a 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 |
---|---|---|
IEnumerable<T> | items | A list of items to enumerate and format in the returned string |
Func<T, string> | value | A function that provides a string representation of an instance of |
string | prefix | A string to create before the string representation of each item in the |
string | suffix | A string to create after the srting representation of each item in the |
Returns
Type | Description |
---|---|
StringBuilder | A comma separated StringBuilder holding each item in the |
Type Parameters
Name | Description |
---|---|
T | The type of object in the |
Remarks
For example:
(new string[] { "One", "Two", "Three" }).ToCommaSeperatedList<string>(x => x, "'", "'").ToString();
Would return:
"'One','Two','Three'"
See Also
ToConcatenatedList(IEnumerable, Func<object, string>, string, string, string)
Returns a collection of items as a 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 |
---|---|---|
IEnumerable | items | A list of items to enumerate and format in the returned string |
Func<object, string> | toString | A function that provides a string representation of an instance of an object in the
|
string | seperator | A string to use to separate each item in the |
string | prefix | A string to create before the string representation of each item in the |
string | suffix | A string to create after the srting representation of each item in the |
Returns
Type | Description |
---|---|
StringBuilder | A StringBuilder holding each item in the |
Remarks
For example:
(new string[] { "One", "Two", "Three" }).ToConcatenatedList(x => x, ", ", "'", "'").ToString();
Would return:
"'One','Two','Three'"
See Also
ToConcatenatedList(IEnumerable, string)
Returns a collection of items as a StringBuilder with a specified seperator
between each item
Declaration
public static StringBuilder ToConcatenatedList(this IEnumerable items, string seperator)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable | items | A list of items to enumerate and format in the returned string |
string | seperator | A string to use to separate each item in the |
Returns
Type | Description |
---|---|
StringBuilder | A StringBuilder holding each item in the |
Remarks
For example:
(new string[] { "One", "Two", "Three" }).ToConcatenatedList(", ").ToString();
Would return:
"One,Two,Three"
See Also
ToConcatenatedList(IEnumerable, string, string, string)
Returns a collection of items as a 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 |
---|---|---|
IEnumerable | items | A list of items to enumerate and format in the returned string |
string | seperator | A string to use to separate each item in the |
string | prefix | A string to create before the string representation of each item in the |
string | suffix | A string to create after the srting representation of each item in the |
Returns
Type | Description |
---|---|
StringBuilder | A StringBuilder holding each item in the |
Remarks
For example:
(new string[] { "One", "Two", "Three" }).ToConcatenatedList(", ", "'", "'").ToString();
Would return:
"'One','Two','Three'"
See Also
ToConcatenatedList<T>(IEnumerable<T>, Func<T, string>, string, string, string)
Returns a collection of items as a 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 |
---|---|---|
IEnumerable<T> | items | A list of items to enumerate and format in the returned string |
Func<T, string> | toString | A function that provides a string representation of an instance of |
string | seperator | A string to use to separate each item in the |
string | prefix | A string to create before the string representation of each item in the |
string | suffix | A string to create after the srting representation of each item in the |
Returns
Type | Description |
---|---|
StringBuilder | A StringBuilder holding each item in the |
Type Parameters
Name | Description |
---|---|
T | The type of object in the |
Remarks
For example:
(new string[] { "One", "Two", "Three" }).ToConcatenatedList<string>(x => x, ", ", "'", "'").ToString();
Would return:
"'One','Two','Three'"
See Also
ToEnumValue(string, string)
Convert a string to an enum value, using the enum's DisplayAttribute values.
Declaration
public static object ToEnumValue(this string self, string typeName)
Parameters
Type | Name | Description |
---|---|---|
string | self | The string which should be a valid display name for the enum type |
string | typeName | A type name string of the enum |
Returns
Type | Description |
---|---|
object |
ToEnumValue<T>(string)
Convert a string to an enum value, using the enum's DisplayAttribute values.
Declaration
public static T ToEnumValue<T>(this string self) where T : Enum
Parameters
Type | Name | Description |
---|---|---|
string | self | The string which should be a valid display name for the enum type |
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T | The type of enum |
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 |
---|---|---|
string | s | The string to return as a variable field equivelant |
Returns
Type | Description |
---|---|
string | The provided |
See Also
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 |
---|---|---|
string | s | The string to return as a variable equivelant |
Returns
Type | Description |
---|---|
string | The provided |
See Also
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 |
---|---|---|
IEnumerable<T> | items | The collection of items to display as rows in a table |
ITreeNode<(string Name, string Key)>[] | columnGroups | Column headers can have heiarchical headers above them. This holds a list of column group
headers. |
(string FileTypeName, string fileType)[] | columns | A list of columns to display in the table |
Func<T, string, string> | getColumnValue | A function that given an item of type |
char | seperator | An optional column separator. By default is | |
Returns
Type | Description |
---|---|
string | A formatted string that holds a table with the specified |
Type Parameters
Name | Description |
---|---|
T | The type of item in the |
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)
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 |
---|---|---|
IEnumerable<T> | items | The collection of items to display as rows in a table |
string[] | columns | A list of columns to display in the table |
Func<T, string, string> | getColumnValue | A function that given an item of type |
char | seperator | An optional column separator. By default is | |
Returns
Type | Description |
---|---|
string | A formatted string that holds a table with the specified |
Type Parameters
Name | Description |
---|---|
T | The type of item in the |
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
Truncate(string, int)
Truncates a string to a specified length
Declaration
public static string Truncate(this string value, int maxLength)
Parameters
Type | Name | Description |
---|---|---|
string | value | |
int | maxLength |
Returns
Type | Description |
---|---|
string |
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 |
---|---|---|
string | s | The string with a name to return as a set of capitalized words |
Returns
Type | Description |
---|---|
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