Table of Contents

Class StringUtil

Namespace
ArcanaDevelopment.adTempus.Shared
Assembly
ArcanaDevelopment.adTempus.Shared.dll

Utilities for working with strings

public static class StringUtil
Inheritance
object
StringUtil

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

NameTypeDescription
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

NameTypeDescription
array string[]
target string

Returns

bool

Remarks

This method uses case-insensitive string comparison.

CaseInsensitiveStringEquals(string, string)

Performs a case-insensitive comparison of two strings.

public static bool CaseInsensitiveStringEquals(string a, string b)

Parameters

NameTypeDescription
a string

A string to compare

b string

A string to compare

Returns

bool

Remarks

Returns true if both are null or empty.

EmptyToDbNull(string)

Returns the original string, or DbNull.Value if the string is empty, null, or whitespace.

public static object EmptyToDbNull(string str)

Parameters

NameTypeDescription
str string

Returns

object

EmptyToNull(string)

Returns the original string, or null if the source is empty, null, or whitespace.

public static string EmptyToNull(string source)

Parameters

NameTypeDescription
source string

The string to convert

Returns

string

The source, or null if source is an empty string.

Remarks

A string containing only whitespace is treated as empty.

See Also

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.

public static string LimitText(string source, int maxLength, bool includeEllipsis)

Parameters

NameTypeDescription
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

public static string ListToString<T>(this IEnumerable<T> source, string delimiter = ",")

Parameters

NameTypeDescription
source IEnumerable<T>
delimiter string

Returns

string

Type Parameters

NameDescription
T

NullToEmpty(string)

Returns the original string, or an empty string if the source is null.

public static string NullToEmpty(string source)

Parameters

NameTypeDescription
source string

The string to convert

Returns

string

The source, or an empty string if source is null.

See Also

RemoveInvalidCharacters(string, string)

Removes characters that are not valid in XML

public static string RemoveInvalidCharacters(string source, string replacement = "")

Parameters

NameTypeDescription
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

NameTypeDescription
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

bool

true if any replacements were made, false otherwise

ReplaceString(string, string, string, StringComparison)

Performs a string replacement using the specified comparison operator (case-insensitive by default).

public static string ReplaceString(string stringToSearch, string oldValue, string newValue, StringComparison comparison = StringComparison.OrdinalIgnoreCase)

Parameters

NameTypeDescription
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

NameTypeDescription
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

NameTypeDescription
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 ". "

public static string StripLineBreaks(string source)

Parameters

NameTypeDescription
source string

The string to convert

Returns

string