Class StringUtility
- Namespace
- ArcanaDevelopment.adTempus.Shared
- Assembly
- ArcanaDevelopment.adTempus.Shared.dll
Utilities for working with strings
public static class StringUtility
- Inheritance
-
objectStringUtility
Methods
AppendString(StringBuilder, string, string)
Appends a string to a StringBuilder, adding a separator if the StringBuilder is not empty.
public static void AppendString(StringBuilder target, string stringToAppend, string separator)
Parameters
| Name | Type | Description |
|---|---|---|
target |
StringBuilder | The StringBuilder to append to |
stringToAppend |
string | The string to append |
separator |
string | The separator to use when appending |
Remarks
This method can be used, for example, to create comma-separated lists.
ArrayContains(string[], string)
Determines whether an array of strings contains the specified value.
public static bool ArrayContains(string[] array, string target)
Parameters
| Name | Type | Description |
|---|---|---|
array |
string[] | |
target |
string |
Returns
- bool
Remarks
This method uses case-insensitive string comparison.
EmptyToDbNull(string)
Returns the original string, or DbNull.Value if the string is empty, null, or whitespace.
public static object EmptyToDbNull(string str)
Parameters
| Name | Type | Description |
|---|---|---|
str |
string |
Returns
- object
EmptyToNull(string)
Returns the original string, or null if the source is empty, null, or whitespace.
[CanBeNull]
public static string EmptyToNull(string source)
Parameters
| Name | Type | Description |
|---|---|---|
source |
string | The string to convert |
Returns
- string
The
source, or null ifsourceis an empty string.
Remarks
A string containing only whitespace is treated as empty.
- See Also
Equals(string, string, StringComparison)
Compares two strings using the specified comparison (OrdinalIgnoreCase by default)
public static bool Equals(string a, string b, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
Parameters
| Name | Type | Description |
|---|---|---|
a |
string | A string to compare |
b |
string | A string to compare |
comparison |
StringComparison |
Returns
- bool
Remarks
Returns true if both are null or empty.
LimitText(string, int, bool)
Truncates a string to the specified length, optionally appending an ellipsis to the string to indicate that it has been truncated.
[NotNull]
public static string LimitText(string source, int maxLength, bool includeEllipsis)
Parameters
| Name | Type | Description |
|---|---|---|
source |
string | The string to limit |
maxLength |
int | The maximum length |
includeEllipsis |
bool | Whether to append an ellipsis (...) if the string is truncated. See Remarks. |
Returns
- string
Remarks
If includeEllipsis is true, the source is shortened to less than maxLength to accommodate the ellipsis (the returned string will never be longer than maxLength).
ListToString<T>(IEnumerable<T>, string)
Converts a list of objects to a delimiter-separated string
[NotNull]
public static string ListToString<T>(this IEnumerable<T> source, string delimiter = ",")
Parameters
| Name | Type | Description |
|---|---|---|
source |
IEnumerable<T> | |
delimiter |
string |
Returns
- string
Type Parameters
| Name | Description |
|---|---|
T |
NullToEmpty(string)
Returns the original string, or an empty string if the source is null.
[NotNull]
public static string NullToEmpty(string source)
Parameters
| Name | Type | Description |
|---|---|---|
source |
string | The string to convert |
Returns
- string
The
source, or an empty string ifsourceis null.
- See Also
RemoveInvalidCharacters(string, string)
Removes characters that are not valid in XML
[ContractAnnotation("source:null=>null; source:notnull=>notnull")]
public static string RemoveInvalidCharacters(string source, string replacement = "")
Parameters
| Name | Type | Description |
|---|---|---|
source |
string | |
replacement |
string |
Returns
- string
ReplaceString(string, string, string, out string, StringComparison)
Performs a string replacement using the specified comparison operator (case-insensitive by default).
public static bool ReplaceString(string stringToSearch, string oldValue, string newValue, out string result, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
Parameters
| Name | Type | Description |
|---|---|---|
stringToSearch |
string | The string to search |
oldValue |
string | The string to search for |
newValue |
string | The replacement string |
result |
string | The resulting string |
comparison |
StringComparison | The comparison type to use |
Returns
ReplaceString(string, string, string, StringComparison)
Performs a string replacement using the specified comparison operator (case-insensitive by default).
[Pure]
public static string ReplaceString(string stringToSearch, string oldValue, string newValue, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
Parameters
| Name | Type | Description |
|---|---|---|
stringToSearch |
string | The string to search |
oldValue |
string | The string to search for |
newValue |
string | The replacement string |
comparison |
StringComparison | The comparison type to use |
Returns
- string
The string with replacements made
StartsWith(string, string, StringComparison)
Tests whether a string starts with another string
public static bool StartsWith(string source, string lookFor, StringComparison comparison)
Parameters
| Name | Type | Description |
|---|---|---|
source |
string | The string to search |
lookFor |
string | The string to look for |
comparison |
StringComparison | The comparison to use |
Returns
- bool
StringContains(string, string, StringComparison)
Tests whether a string contains another string.
public static bool StringContains(string source, string lookFor, StringComparison comparison)
Parameters
| Name | Type | Description |
|---|---|---|
source |
string | The string to search |
lookFor |
string | The string to look for |
comparison |
StringComparison | The comparison to use |
Returns
- bool
StripLineBreaks(string)
Removes newlines from a string, replacing them with ". "
[ContractAnnotation("source:null=>null; source:notnull=>notnull")]
public static string StripLineBreaks(string source)
Parameters
| Name | Type | Description |
|---|---|---|
source |
string | The string to convert |
Returns
- string