Class StringUtil
Provides a comprehensive set of string utility methods and extension methods for common string operations used throughout Scylla, including stable hashing, sanitization, JSON escaping, null/whitespace guards, ordinal comparison helpers, safe slicing, line-ending normalization, invariant-culture formatting and parsing, truncation, padding, and Unity Rich Text tag generation.
Inherited Members
Namespace: Scylla.Core.Util
Assembly: ScyllaCore.dll
Syntax
public static class StringUtil
Remarks
All string comparison and search methods default to Ordinal rather than culture-sensitive comparison. This is intentional: ordinal comparisons are faster, produce consistent results across locales, and are appropriate for identifiers, file paths, and serialized data - the primary use cases in a game framework.
Extension methods are defined on string and can be invoked directly on
any string instance (e.g., myString.SafeSubstring(0, 10)).
Static (non-extension) helpers take their primary string as the first parameter.
The FNV-1a hash produced by GetStableHash32(string, bool) is deterministic across
platforms and process restarts, unlike string.GetHashCode() which is
randomized in .NET 5+ to prevent hash-flooding attacks.
Methods
Bold(string)
Wraps the given string in HTML bold tags. If the input is null, an empty string is wrapped in bold tags instead.
Declaration
public static string Bold(string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The input string to be wrapped in bold tags; may be null. |
Returns
| Type | Description |
|---|---|
| string | A string wrapped in bold tags. If the input is null, an empty bold tag is produced. |
Color(string, Color32, bool)
Wraps the given text in a color tag using the specified color, optionally including the alpha channel.
Declaration
public static string Color(string text, Color32 color, bool includeAlpha = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The string to be wrapped in the color tag. If null, an empty string is used. |
| Color32 | color | The color to apply to the text, represented as a UnityEngine.Color32. |
| bool | includeAlpha | Determines whether the alpha channel should be included in the color's hex representation. Default is true. |
Returns
| Type | Description |
|---|---|
| string | A string wrapped in a color tag with the specified color. |
ContainsOrdinal(string, string)
Determines whether the specified string contains the given value using ordinal comparison.
Declaration
public static bool ContainsOrdinal(this string s, string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to search within; can be null. |
| string | value | The string to search for; can be null. |
Returns
| Type | Description |
|---|---|
| bool |
|
ContainsOrdinalIgnoreCase(string, string)
Determines whether the specified string contains the given value using an ordinal, case-insensitive comparison.
Declaration
public static bool ContainsOrdinalIgnoreCase(this string s, string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to search within. |
| string | value | The string to locate within |
Returns
| Type | Description |
|---|---|
| bool |
|
CountOccurrences(string, string, StringComparison)
Counts the number of occurrences of a specified substring within the given string, based on the specified string comparison option.
Declaration
public static int CountOccurrences(this string s, string value, StringComparison comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string in which to search for the substring; can be null or empty. |
| string | value | The substring to count occurrences of; cannot be null or empty. |
| StringComparison | comparison | The string comparison option to use while matching the substring. |
Returns
| Type | Description |
|---|---|
| int | The number of times the substring appears in the string, or 0 if the string is null or empty. |
EmptyIfNull(string)
Returns the specified string, or an empty string if the input is null.
Declaration
public static string EmptyIfNull(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to check; may be null. |
Returns
| Type | Description |
|---|---|
| string | The original string if not null, otherwise an empty string. |
EndsWithOrdinal(string, string)
Determines whether the end of the specified string instance matches the specified suffix, using ordinal comparison.
Declaration
public static bool EndsWithOrdinal(this string s, string suffix)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to evaluate; may be null. |
| string | suffix | The string to compare to the end of the input string; cannot be null. |
Returns
| Type | Description |
|---|---|
| bool | True if the string ends with the specified suffix using ordinal comparison; otherwise, false. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
EndsWithOrdinalIgnoreCase(string, string)
Determines whether the end of the specified string instance matches a specified suffix, using a case-insensitive ordinal comparison.
Declaration
public static bool EndsWithOrdinalIgnoreCase(this string s, string suffix)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to examine. This can be null, in which case the method returns false. |
| string | suffix | The suffix to compare to the end of this string. Cannot be null. |
Returns
| Type | Description |
|---|---|
| bool | True if the end of the string matches the specified suffix; otherwise, false. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the suffix is null. |
EqualsOrdinal(string, string)
Determines whether two strings are equal using ordinal comparison.
Declaration
public static bool EqualsOrdinal(string a, string b)
Parameters
| Type | Name | Description |
|---|---|---|
| string | a | The first string to compare; may be null. |
| string | b | The second string to compare; may be null. |
Returns
| Type | Description |
|---|---|
| bool |
|
EqualsOrdinalIgnoreCase(string, string)
Determines whether two strings are equal, using a case-insensitive ordinal comparison.
Declaration
public static bool EqualsOrdinalIgnoreCase(string a, string b)
Parameters
| Type | Name | Description |
|---|---|---|
| string | a | The first string to compare; may be null. |
| string | b | The second string to compare; may be null. |
Returns
| Type | Description |
|---|---|
| bool | True if both strings are equal when compared using a case-insensitive ordinal comparison; otherwise, false. |
EscapeJSONString(string)
Escapes special characters in a string for use in JSON string values. Escapes quotes, backslashes, control characters, and common escape sequences according to JSON specification.
Declaration
public static string EscapeJSONString(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The string value to escape. Can be null or empty. |
Returns
| Type | Description |
|---|---|
| string | The escaped JSON string safe for embedding in JSON. Returns an empty string if the input is null or empty. |
Remarks
This method performs manual JSON string escaping for performance (no external dependencies). Escapes the following characters:
- " (quote) -> "
- \ (backslash) -> \
- \b (backspace) -> \b
- \f (form feed) -> \f
- \n (newline) -> \n
- \r (carriage return) -> \r
- \t (tab) -> \t
- Control characters (0x00-0x1F) -> \uXXXX
EscapeRichText(string)
Escapes special characters in a string for use in rich text by replacing '&', '<', and '>' with their corresponding HTML character entities.
Declaration
public static string EscapeRichText(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to escape; may be null. |
Returns
| Type | Description |
|---|---|
| string | A new string with '&', '<', and '>' replaced by "&", "<", and ">" respectively, or the original string if no special characters are present. Returns null if the input is null. |
GetStableHash32(string, bool)
Computes a stable, deterministic 32-bit hash for the specified string using the
FNV-1a (Fowler-Noll-Vo) algorithm. Unlike string.GetHashCode(), which is
randomized per-process in .NET 5+ to mitigate hash-flooding attacks, this hash
produces the same value for the same input across all platforms, runtimes, and
application restarts.
Declaration
public static uint GetStableHash32(string s, bool ignoreCase = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to hash. Returns |
| bool | ignoreCase |
|
Returns
| Type | Description |
|---|---|
| uint | A deterministic 32-bit unsigned integer hash of the string, or |
Remarks
FNV-1a is not a cryptographic hash; do not use it for security-sensitive
purposes. It is well-suited for dictionary keys, content-addressable caches,
and fast string-to-integer mapping in game logic. The offset basis is
2166136261 and the prime multiplier is 16777619.
When ignoreCase is true, each character is
converted to uppercase via ToUpperInvariant(char) before
hashing, ensuring that "Hello" and "hello" produce the same
hash regardless of locale.
HasText(string)
Determines whether the given string contains non-whitespace characters.
Declaration
public static bool HasText(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to evaluate; may be null, empty, or contain only whitespace. |
Returns
| Type | Description |
|---|---|
| bool | True if the string is not null, not empty, and contains at least one non-whitespace character; otherwise, false. |
IndentLines(string, string, bool)
Indents each line of the given string by the specified indent string. Optionally, empty lines can also be indented.
Declaration
public static string IndentLines(this string s, string indent, bool indentEmptyLines = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string whose lines are to be indented. |
| string | indent | The string to prepend to the beginning of each line. |
| bool | indentEmptyLines | Indicates whether to prepend the indent string to empty lines; if false, empty lines remain unchanged. |
Returns
| Type | Description |
|---|---|
| string | A new string with each line indented by the specified indent string. If the input string is null, returns null. |
IndexOf(string, string, StringComparison)
Returns the zero-based index of the first occurrence of a specified string in the current string, using the specified comparison option.
Declaration
public static int IndexOf(this string s, string value, StringComparison comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string instance to search; may be null. |
| string | value | The string to locate within the current string; cannot be null. |
| StringComparison | comparison | The comparison rules to use when locating the string. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index position of the first occurrence of the specified string, or -1 if the string is not found or the input string is null. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the value parameter is null. |
IsValidFileName(string)
Determines whether a string is usable as a file name on every major platform (Windows, macOS, and Linux).
Declaration
public static bool IsValidFileName(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The file name to test, without any directory component. May include an extension. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
A name is rejected when it is null, empty, or whitespace-only, when
it contains a character rejected by IsValidFileNameChar(char), when
it ends in a period or a space (Windows silently strips both, so the name
written is not the name requested), or when its stem matches one of the
Windows reserved device names.
The reserved names are CON, PRN, AUX, NUL,
COM1 through COM9, and LPT1 through LPT9. The
comparison is case-insensitive and applies to the stem only, so both
"nul" and "NUL.txt" are rejected while "NULL" and
"CONFIG" are accepted.
This is the whole-name counterpart to IsValidFileNameChar(char). To repair a name rather than reject it, use SanitizeForFileName(string, char) (substitutes a replacement character) or StripInvalidFileNameChars(string, bool) (removes offending characters).
See Also
IsValidFileNameChar(char)
Determines whether a single character is legal inside a file name on every major platform (Windows, macOS, and Linux).
Declaration
public static bool IsValidFileNameChar(char c)
Parameters
| Type | Name | Description |
|---|---|---|
| char | c | The character to test. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
Letters outside ASCII (umlauts, accents, CJK) and punctuation such as the
apostrophe are legal on all three platforms and therefore return true.
Whether a character is legal in a given position is a separate
question: a trailing period or space is rejected on Windows even though both
characters pass this test. Use IsValidFileName(string) for the
whole-name rules.
See Also
Italic(string)
Wraps the given text in HTML italic tags ("<i>").
Declaration
public static string Italic(string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to wrap; can be null. |
Returns
| Type | Description |
|---|---|
| string | The input text wrapped in HTML italic tags, or an empty italic tag if the input is null. |
Left(string, int)
Returns the left-most portion of the string with the specified number of characters. If the specified count exceeds the string's length, the entire string is returned.
Declaration
public static string Left(this string s, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string from which to extract the left substring. |
| int | count | The number of characters to include in the left substring. |
Returns
| Type | Description |
|---|---|
| string | A substring containing the left-most characters of the input string, or the original string if the count exceeds its length. |
NormalizeLineEndings(string, string)
Normalizes all line endings in the specified string to the provided newline format. Converts Windows-style ("\r\n") and old Mac-style ("\r") line endings to Unix-style ("\n") before applying the specified newline format.
Declaration
public static string NormalizeLineEndings(this string s, string newline = "\n")
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string whose line endings are to be normalized; may be null. |
| string | newline | The desired newline format to replace normalized line endings with. Defaults to Unix-style ("\n"). |
Returns
| Type | Description |
|---|---|
| string | The input string with normalized line endings converted to the specified newline format. If the input string is null, returns null. If the input string is empty, returns an empty string. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
NullIfEmpty(string)
Returns null if the input string is null or empty; otherwise, returns the original string.
Declaration
public static string NullIfEmpty(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to evaluate. |
Returns
| Type | Description |
|---|---|
| string | Null if the input string is null or empty; otherwise, the original string. |
NullIfWhiteSpace(string)
Returns null if the input string is null, empty, or consists only of white-space characters; otherwise, returns the original string.
Declaration
public static string NullIfWhiteSpace(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to evaluate; may be null. |
Returns
| Type | Description |
|---|---|
| string | Null if the input string is null, empty, or contains only white-space characters; otherwise, the original string. |
Remove(string, string, StringComparison)
Removes all occurrences of the specified string from the input string using the specified string comparison.
Declaration
public static string Remove(this string s, string value, StringComparison comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string from which occurrences of the specified value will be removed. |
| string | value | The string value to remove from the input string. |
| StringComparison | comparison | The type of string comparison to use for matching occurrences to remove. |
Returns
| Type | Description |
|---|---|
| string | A new string with all matching occurrences of the specified value removed, or the original string if no matches are found. |
Replace(string, string, string, StringComparison)
Replaces all occurrences of a specified string in the current string instance with another specified string, using the provided string comparison option.
Declaration
public static string Replace(this string s, string oldValue, string newValue, StringComparison comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string in which replacements will be made. May be null. |
| string | oldValue | The string to be replaced. Cannot be null or empty. |
| string | newValue | The string to replace all occurrences of |
| StringComparison | comparison | The comparison option to use for matching strings. |
Returns
| Type | Description |
|---|---|
| string | A new string with all occurrences of |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
| ArgumentException | Thrown when |
Right(string, int)
Returns the specified number of characters from the end of the string. If the string is null or empty, or the count is less than or equal to zero, an empty string is returned.
Declaration
public static string Right(this string s, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to process; may be null or empty. |
| int | count | The number of characters to retrieve from the end of the string. |
Returns
| Type | Description |
|---|---|
| string | A string containing the last |
SafePadCenter(string, int, char)
Pads the string on both sides to center it within the specified total width. If the padding cannot be evenly distributed, the extra character is added to the right. Safely handles null strings.
Declaration
public static string SafePadCenter(this string s, int totalWidth, char paddingChar = ' ')
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to center; may be null. |
| int | totalWidth | The desired total width of the resulting string. |
| char | paddingChar | The character to use for padding. Default is space. |
Returns
| Type | Description |
|---|---|
| string | The string centered within the specified width. If the string is already at least as long as totalWidth, the original string is returned unchanged. Returns a string of padding characters if input is null. |
SafePadLeft(string, int, char)
Pads the string on the left to the specified total width using the specified padding character. Unlike the built-in PadLeft(int), this method safely handles null strings.
Declaration
public static string SafePadLeft(this string s, int totalWidth, char paddingChar = ' ')
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to pad; may be null. |
| int | totalWidth | The desired total width of the resulting string. |
| char | paddingChar | The character to use for padding. Default is space. |
Returns
| Type | Description |
|---|---|
| string | The string padded on the left to the specified width. If the string is already at least as long as totalWidth, the original string is returned unchanged. Returns a string of padding characters if input is null. |
SafePadRight(string, int, char)
Pads the string on the right to the specified total width using the specified padding character. Unlike the built-in PadRight(int), this method safely handles null strings.
Declaration
public static string SafePadRight(this string s, int totalWidth, char paddingChar = ' ')
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to pad; may be null. |
| int | totalWidth | The desired total width of the resulting string. |
| char | paddingChar | The character to use for padding. Default is space. |
Returns
| Type | Description |
|---|---|
| string | The string padded on the right to the specified width. If the string is already at least as long as totalWidth, the original string is returned unchanged. Returns a string of padding characters if input is null. |
SafeSubstring(string, int, int)
Returns a substring of the input string starting at the specified index and with the specified length. Safely handles cases where the input string is null, empty, or the specified range is out of bounds.
Declaration
public static string SafeSubstring(this string s, int startIndex, int length = 2147483647)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string from which to extract the substring. |
| int | startIndex | The zero-based starting index of the substring. Values less than zero are clamped to zero. |
| int | length | The length of the substring. Defaults to the maximum available length. Values less than or equal to zero return an empty string. |
Returns
| Type | Description |
|---|---|
| string | The substring of the input string corresponding to the specified range, or an empty string if the range is invalid or the input string is null/empty. |
SanitizeForFileName(string, char)
Sanitizes the input string to make it a valid file name by replacing invalid characters with the specified replacement character and removing invalid trailing characters. Uses a cross-platform set of invalid characters to ensure the sanitized filename works on both Windows and macOS/Unix platforms.
Declaration
public static string SanitizeForFileName(this string s, char replacement = '_')
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to sanitize. Can be null. |
| char | replacement | The character to replace invalid file name characters with. Defaults to '_'. |
Returns
| Type | Description |
|---|---|
| string | A sanitized string suitable for use as a file name. Returns an empty string if the input is null or results in an empty string after sanitization. |
Size(string, int)
Wraps the specified text in a size tag with the provided size value.
Declaration
public static string Size(string text, int size)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to wrap; may be null. |
| int | size | The size value to apply; must be greater than 0. |
Returns
| Type | Description |
|---|---|
| string | The text wrapped in size tags, or an empty size tag if the text is null. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if the size is less than or equal to 0. |
SplitCamelCase(string, char)
Inserts a separator at every word boundary in a camelCase or PascalCase identifier.
Declaration
public static string SplitCamelCase(this string s, char separator = ' ')
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The identifier to split. |
| char | separator | The character to insert at each boundary. |
Returns
| Type | Description |
|---|---|
| string | The identifier with separators inserted, or the original string when it has no boundary. |
Remarks
Purely mechanical, and deliberately has no opinion about casing: the result keeps the characters it was given and only gains separators. A caller wanting title case or upper case applies it afterwards.
Three boundaries are recognized. An ordinary hump, where a lowercase letter is followed
by an uppercase one, so upArrow becomes up Arrow. The end of a run of
capitals, recognized as the last capital before a lowercase letter, so
XRController becomes XR Controller rather than X R Controller. And
a digit following a word of more than one letter, so numpad1 becomes
numpad 1 while f1 is left alone, because a single letter before a number
is nearly always one name rather than two.
Nothing is inserted after a character that is neither a letter nor a digit, so text that already contains separators or punctuation is not given a second one.
The builder is allocated only once a boundary is actually found, so a single-word identifier, which is the common case, returns the original instance and allocates nothing.
StartsWithOrdinal(string, string)
Determines whether the beginning of the specified string instance matches the specified prefix using ordinal comparison.
Declaration
public static bool StartsWithOrdinal(this string s, string prefix)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to compare; if null, the method returns false. |
| string | prefix | The prefix to compare against; cannot be null. |
Returns
| Type | Description |
|---|---|
| bool | True if the beginning of the string instance matches the prefix using ordinal comparison; otherwise, false. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the prefix is null. |
StartsWithOrdinalIgnoreCase(string, string)
Determines if the specified string starts with the given prefix, using case-insensitive ordinal comparison.
Declaration
public static bool StartsWithOrdinalIgnoreCase(this string s, string prefix)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to check; may be null. |
| string | prefix | The prefix to compare; should not be null. |
Returns
| Type | Description |
|---|---|
| bool | True if the string starts with the specified prefix; otherwise, false. Returns false if the input string is null. |
Strikethrough(string)
Adds a strikethrough effect to the specified text by wrapping it in strikethrough tags.
Declaration
public static string Strikethrough(string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to be wrapped in strikethrough tags. If null, an empty string will be used. |
Returns
| Type | Description |
|---|---|
| string | The input text wrapped in " |
StripInvalidFileNameChars(string, bool)
Removes every character that is illegal in a cross-platform file name, along with any resulting trailing periods and spaces.
Declaration
public static string StripInvalidFileNameChars(string name, bool trimTrailing = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The string to filter. May be |
| bool | trimTrailing |
|
Returns
| Type | Description |
|---|---|
| string | A copy of |
Remarks
This differs from SanitizeForFileName(string, char) in what it does with a
rejected character: SanitizeForFileName substitutes a replacement
character ('_' by default), this method drops it. Removal is the
right behavior for a live text-field filter, where substitution would
insert an underscore on every rejected keystroke.
Pass false for trimTrailing when filtering a
field as the user types. The trailing rule is a whole-string rule, and
applying it per keystroke makes a name ending in a space or a period
impossible to type: the character is removed the moment it is entered, so
the user can never get to the one that would follow it. Filter per
keystroke, trim once on commit.
The result may still be a Windows reserved device name; that rule cannot be enforced per character. Check the committed value with IsValidFileName(string).
See Also
StripRichTextTags(string)
Removes all rich-text tags (HTML-like tags enclosed in angle brackets) from the specified string, returning the plain text content.
Declaration
public static string StripRichTextTags(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string that may contain rich-text tags; may be null. |
Returns
| Type | Description |
|---|---|
| string | The input string with all rich-text tags removed, or null if the input string is null. |
ToStringInvariant(decimal)
Converts the specified decimal value to its string representation using the invariant culture.
Declaration
public static string ToStringInvariant(decimal value)
Parameters
| Type | Name | Description |
|---|---|---|
| decimal | value | The decimal value to convert. |
Returns
| Type | Description |
|---|---|
| string | A string representation of the decimal value using the invariant culture. |
ToStringInvariant(double)
Converts a double value to its string representation using the invariant culture.
Declaration
public static string ToStringInvariant(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| double | value | The double value to convert. |
Returns
| Type | Description |
|---|---|
| string | The string representation of the double value in invariant culture format. |
ToStringInvariant(int)
Converts the specified integer value to its string representation using invariant culture.
Declaration
public static string ToStringInvariant(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The integer value to convert. |
Returns
| Type | Description |
|---|---|
| string | The string representation of the integer, formatted using invariant culture. |
ToStringInvariant(long)
Converts the specified long value to its string representation using the invariant culture format.
Declaration
public static string ToStringInvariant(long value)
Parameters
| Type | Name | Description |
|---|---|---|
| long | value | The long value to convert to a string. |
Returns
| Type | Description |
|---|---|
| string | The string representation of the specified value in invariant culture format. |
ToStringInvariant(object)
Converts the given object to its string representation using the invariant culture. If the object is null, it returns a predefined "Null" string.
Declaration
public static string ToStringInvariant(object value)
Parameters
| Type | Name | Description |
|---|---|---|
| object | value | The object to be converted to a string; can be null. |
Returns
| Type | Description |
|---|---|
| string | The string representation of the object in invariant culture, or "Null" if the object is null. |
ToStringInvariant(float)
Converts a floating-point value to its string representation using invariant culture formatting.
Declaration
public static string ToStringInvariant(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value | The floating-point value to convert. |
Returns
| Type | Description |
|---|---|
| string | A string representation of the value formatted using invariant culture. |
TrimToEmpty(string)
Trims the input string and returns the result, or an empty string if the input is null.
Declaration
public static string TrimToEmpty(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to trim; may be null. |
Returns
| Type | Description |
|---|---|
| string | A trimmed version of the string if not null, otherwise an empty string. |
TrimToNull(string)
Trims the input string of leading and trailing whitespace, returning null if the resulting string is either null or empty.
Declaration
public static string TrimToNull(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The input string to trim; may be null or consist of only whitespace. |
Returns
| Type | Description |
|---|---|
| string | The trimmed string if it contains non-whitespace characters, otherwise null. |
Truncate(string, int, string)
Truncates the string to the specified maximum length, appending an ellipsis indicator if truncation occurs. The ellipsis is included in the maximum length.
Declaration
public static string Truncate(this string s, int maxLength, string ellipsis = "...")
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to truncate; may be null. |
| int | maxLength | Maximum length of the result including ellipsis. Must be at least 1. |
| string | ellipsis | The ellipsis string to append when truncating. Default is "...". |
Returns
| Type | Description |
|---|---|
| string | The truncated string with ellipsis if the original exceeded maxLength, or the original string if it was shorter. Returns empty string if input is null. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if maxLength is less than 1. |
| ArgumentNullException | Thrown if ellipsis is null. |
TryParseBool(string, out bool)
Attempts to convert a human-readable string representation to its bool
equivalent, accepting a broader set of truthy and falsy representations than
TryParse(ReadOnlySpan<char>, out bool).
Declaration
public static bool TryParseBool(string s, out bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to parse. |
| bool | value | When this method returns |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
The following string values are recognised (case-insensitive, leading/trailing whitespace trimmed):
- Truthy:
true,t,yes,y,on,1 - Falsy:
false,f,no,n,off,0
Any value not in the above table is passed to TryParse(ReadOnlySpan<char>, out bool) as a final fallback.
TryParseEnum<TEnum>(string, bool, out TEnum)
Attempts to parse the specified string into an enumeration value of the given enum type.
Declaration
public static bool TryParseEnum<TEnum>(string s, bool ignoreCase, out TEnum value) where TEnum : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string representation of the enumeration value to parse. |
| bool | ignoreCase | A boolean value indicating whether to ignore case during the parsing. |
| TEnum | value | When this method returns, contains the parsed enumeration value of type |
Returns
| Type | Description |
|---|---|
| bool | True if the string was successfully parsed into a value of the specified enumeration type; otherwise, false. |
Type Parameters
| Name | Description |
|---|---|
| TEnum | The type of the enumeration to parse into. Must be a struct and an Enum. |
TryParseFloatInvariant(string, out float)
Attempts to parse the specified string as a floating-point number using the invariant culture.
Declaration
public static bool TryParseFloatInvariant(string s, out float value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to parse, representing a floating-point number in invariant culture format. |
| float | value | When this method returns, contains the parsed floating-point value if the conversion succeeded, or 0 if it failed. |
Returns
| Type | Description |
|---|---|
| bool | True if the string was successfully parsed as a floating-point number; otherwise, false. |
TryParseInt(string, out int)
Attempts to convert the specified string representation of a number to its integer equivalent.
Declaration
public static bool TryParseInt(string s, out int value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string containing the number to parse. |
| int | value | When this method returns, contains the integer value equivalent to the number contained in
|
Returns
| Type | Description |
|---|---|
| bool | True if the string was successfully converted; otherwise, false. |
Underline(string)
Wraps the specified text in HTML underline tags (<u>).
Declaration
public static string Underline(string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The input text to wrap; if null, an empty string will be used. |
Returns
| Type | Description |
|---|---|
| string | The text wrapped in underline tags. |