Class StringExtensions
String extension methods.
Inheritance
Namespace: Umbraco.Extensions
Assembly: Umbraco.Core.dll
Syntax
public static class StringExtensions
Methods
View SourceAppendQueryStringToUrl(string, params string[])
Appends one or more query strings to a URL.
Declaration
public static string AppendQueryStringToUrl(this string url, params string[] queryStrings)
Parameters
| Type | Name | Description |
|---|---|---|
| string | url | The base URL to append query strings to. |
| string[] | queryStrings | The query strings to append. |
Returns
| Type | Description |
|---|---|
| string | The URL with the query strings appended. |
Remarks
This method ensures that the resulting URL is structured correctly, that there is only one '?' and that parameters are delimited properly with '&'.
CleanForXss(string, params char[])
Cleans a string to aid in preventing XSS attacks.
Declaration
public static string CleanForXss(this string input, params char[] ignoreFromClean)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to clean. |
| char[] | ignoreFromClean | Characters to ignore when cleaning. |
Returns
| Type | Description |
|---|---|
| string | The cleaned string with HTML stripped and potentially dangerous characters removed. |
ContainsAny(string, IEnumerable<string>, StringComparison)
Checks whether a string haystack contains within it any of the strings in the needles collection.
Declaration
public static bool ContainsAny(this string haystack, IEnumerable<string> needles, StringComparison comparison = StringComparison.CurrentCulture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | haystack | The string to check. |
| IEnumerable<string> | needles | The collection of strings to check are contained within the first string. |
| StringComparison | comparison | The type of comparison to perform. Defaults to System.StringComparison.CurrentCulture. |
Returns
| Type | Description |
|---|---|
| bool |
|
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when haystack is null. |
ConvertToHex(string)
Converts a string to its hexadecimal representation.
Declaration
public static string ConvertToHex(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to convert. |
Returns
| Type | Description |
|---|---|
| string | The hexadecimal representation of the string, or an empty string if the input is null or empty. |
CountOccurrences(string, string)
Counts the number of occurrences of a substring within a string.
Declaration
public static int CountOccurrences(this string haystack, string needle)
Parameters
| Type | Name | Description |
|---|---|---|
| string | haystack | The string to search within. |
| string | needle | The substring to count. |
Returns
| Type | Description |
|---|---|
| int | The number of times the needle appears in the haystack. |
Remarks
Having benchmarked various solutions (including for/foreach, split and LINQ-based ones), this is by far the fastest way to find string needles in a string haystack.
CsvContains(string, string)
Determines whether a comma-separated value string contains the specified value.
Declaration
public static bool CsvContains(this string csv, string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | csv | The comma-separated string to search. |
| string | value | The value to search for. |
Returns
| Type | Description |
|---|---|
| bool |
|
DecodeFromHex(string)
Decodes a hexadecimal string back to its original string representation.
Declaration
public static string DecodeFromHex(this string hexValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | hexValue | The hexadecimal string to decode. |
Returns
| Type | Description |
|---|---|
| string | The decoded string. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the hex string has an odd length. |
DetectIsEmptyJson(string)
Determines whether a string represents empty JSON (either an empty array or empty object).
Declaration
public static bool DetectIsEmptyJson(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
DetectIsJson(string)
Attempts to detect whether a string is JSON by checking for opening and closing brackets or braces.
Declaration
public static bool DetectIsJson(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
This is not a fail-safe way to detect JSON, but it is quicker than doing a try/catch when deserialising when it is not JSON.
EncodeAsGuid(string)
Encodes a string as a GUID by converting it to hexadecimal and parsing as a GUID.
Declaration
public static Guid EncodeAsGuid(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to encode. |
Returns
| Type | Description |
|---|---|
| Guid | A GUID representation of the string, or System.Guid.Empty if conversion fails. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the input is null or whitespace. |
EncodeJsString(string)
Encodes a string for safe use within JavaScript code by escaping special characters.
Declaration
public static string EncodeJsString(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to encode. |
Returns
| Type | Description |
|---|---|
| string | The JavaScript-encoded string with special characters escaped. |
EnsureCultureCode(string?)
Verifies the provided string is a valid culture code and returns it with consistent casing.
Declaration
public static string? EnsureCultureCode(this string? culture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | culture | The culture code to validate and normalise. |
Returns
| Type | Description |
|---|---|
| string | The culture code in standard casing, or the original value if null, empty, or the wildcard "*". |
Exceptions
| Type | Condition |
|---|---|
| CultureNotFoundException | Thrown when the culture code is not valid. |
EnsureEndsWith(string, char)
Ensures that the string ends with the specified character.
Declaration
public static string EnsureEndsWith(this string input, char value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to check. |
| char | value | The character that the input should end with. |
Returns
| Type | Description |
|---|---|
| string | The input string if it already ends with the specified character; otherwise, the character appended to the input. |
EnsureEndsWith(string, string)
Ensures that the string ends with the specified string.
Declaration
public static string EnsureEndsWith(this string input, string toEndWith)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to check. |
| string | toEndWith | The string that the input should end with. |
Returns
| Type | Description |
|---|---|
| string | The input string if it already ends with the specified value; otherwise, the value appended to the input. |
EnsureStartsWith(string, char)
Ensures that the string starts with the specified character.
Declaration
public static string EnsureStartsWith(this string input, char value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to check. |
| char | value | The character that the input should start with. |
Returns
| Type | Description |
|---|---|
| string | The input string if it already starts with the specified character; otherwise, the character prepended to the input. |
EnsureStartsWith(string, string)
Ensures that the string starts with the specified string.
Declaration
public static string EnsureStartsWith(this string input, string toStartWith)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to check. |
| string | toStartWith | The string that the input should start with. |
Returns
| Type | Description |
|---|---|
| string | The input string if it already starts with the specified value; otherwise, the value prepended to the input. |
EnumParse<T>(string, bool)
Parses a string to an enumeration value.
Declaration
public static T EnumParse<T>(this string strType, bool ignoreCase)
Parameters
| Type | Name | Description |
|---|---|---|
| string | strType | The string value to parse. |
| bool | ignoreCase | A value indicating whether to ignore case when parsing. |
Returns
| Type | Description |
|---|---|
| T | The parsed enumeration value. |
Type Parameters
| Name | Description |
|---|---|
| T | The enumeration type to parse to. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the string cannot be parsed to the specified enumeration type. |
EnumTryParse<T>(string, bool, out T?)
Attempts to parse a string to an enumeration value.
Declaration
public static bool EnumTryParse<T>(this string strType, bool ignoreCase, out T? result)
Parameters
| Type | Name | Description |
|---|---|---|
| string | strType | The string value to parse. |
| bool | ignoreCase | A value indicating whether to ignore case when parsing. |
| T | result | When this method returns, contains the parsed enumeration value if successful; otherwise, the default value. |
Returns
| Type | Description |
|---|---|
| bool |
|
Type Parameters
| Name | Description |
|---|---|
| T | The enumeration type to parse to. |
EscapeRegexSpecialCharacters(string)
Escapes all regular expression special characters in the string by prefixing them with a backslash.
Declaration
public static string EscapeRegexSpecialCharacters(this string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The string to escape. |
Returns
| Type | Description |
|---|---|
| string | The string with all regex special characters escaped. |
EscapedSplit(string, char, char)
Splits a string on a specified character while supporting an escape character to include the split character in values.
Declaration
public static IEnumerable<string> EscapedSplit(this string value, char splitChar, char escapeChar = '\\')
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The string to split. |
| char | splitChar | The character to split on. |
| char | escapeChar | The character used to escape the split character. Defaults to backslash. |
Returns
| Type | Description |
|---|---|
| IEnumerable<string> | An enumerable of substrings delimited by the split character, with escape sequences resolved. |
ExceptChars(string, HashSet<char>)
Returns a new string with all characters from the specified exclusion set removed.
Declaration
public static string ExceptChars(this string str, HashSet<char> toExclude)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The string to filter. |
| HashSet<char> | toExclude | The set of characters to remove from the string. |
Returns
| Type | Description |
|---|---|
| string | The filtered string with excluded characters removed. |
FromUrlBase64(string)
Decodes a URL-safe base64 string back to its original string representation.
Declaration
public static string? FromUrlBase64(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The URL-safe base64 string to decode. |
Returns
| Type | Description |
|---|---|
| string | The decoded string, or null if decoding fails. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the input is null. |
GenerateHash(string)
Generates a hash of a string using SHA1.
Declaration
public static string GenerateHash(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The string to hash. |
Returns
| Type | Description |
|---|---|
| string | The SHA1 hash of the string as a hexadecimal string. |
GenerateHash<T>(string)
Generates a hash of a string using the specified hash algorithm type.
Declaration
public static string GenerateHash<T>(this string str) where T : HashAlgorithm
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The string to hash. |
Returns
| Type | Description |
|---|---|
| string | The hash of the string as a hexadecimal string. |
Type Parameters
| Name | Description |
|---|---|
| T | The hash algorithm implementation to use. |
GetFileExtension(string)
Determines the extension of the path or URL.
Declaration
public static string GetFileExtension(this string file)
Parameters
| Type | Name | Description |
|---|---|---|
| string | file | The file path or URL to extract the extension from. |
Returns
| Type | Description |
|---|---|
| string | The file extension including the leading period, or an empty string if no extension is found. |
GetIdsFromPathReversed(string)
Converts a path string to an array of node IDs in reverse order (deepest to shallowest).
Declaration
public static int[] GetIdsFromPathReversed(this string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The path string, expected as a comma-delimited collection of integers. |
Returns
| Type | Description |
|---|---|
| int[] | An array of integers matching the provided path, in reverse order. |
IfNullOrWhiteSpace(string?, string?)
Returns the default value if the string is null or whitespace; otherwise, returns the original string.
Declaration
public static string? IfNullOrWhiteSpace(this string? str, string? defaultValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The string to check. |
| string | defaultValue | The default value to return if the string is null or whitespace. |
Returns
| Type | Description |
|---|---|
| string | The default value if the string is null or whitespace; otherwise, the original string. |
InvariantContains(IEnumerable<string>, string)
Determines whether the collection contains the specified string using case-insensitive invariant culture comparison.
Declaration
public static bool InvariantContains(this IEnumerable<string> compare, string compareTo)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<string> | compare | The collection of strings to search within. |
| string | compareTo | The string to search for. |
Returns
| Type | Description |
|---|---|
| bool |
|
InvariantContains(string, string)
Determines whether the string contains the specified substring using case-insensitive ordinal comparison.
Declaration
public static bool InvariantContains(this string compare, string compareTo)
Parameters
| Type | Name | Description |
|---|---|---|
| string | compare | The string to search within. |
| string | compareTo | The substring to search for. |
Returns
| Type | Description |
|---|---|
| bool |
|
InvariantEndsWith(string, string)
Determines whether the end of this string matches the specified string using case-insensitive invariant culture comparison.
Declaration
public static bool InvariantEndsWith(this string compare, string compareTo)
Parameters
| Type | Name | Description |
|---|---|---|
| string | compare | The string to check. |
| string | compareTo | The string to compare to the end of this string. |
Returns
| Type | Description |
|---|---|
| bool |
|
InvariantEquals(string?, string?)
Compares two strings for equality using case-insensitive invariant culture comparison.
Declaration
public static bool InvariantEquals(this string? compare, string? compareTo)
Parameters
| Type | Name | Description |
|---|---|---|
| string | compare | The first string to compare. |
| string | compareTo | The second string to compare. |
Returns
| Type | Description |
|---|---|
| bool |
|
InvariantFormat(string?, params object?[])
Formats the string using the invariant culture.
Declaration
public static string InvariantFormat(this string? format, params object?[] args)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format | The composite format string. |
| object[] | args | The objects to format. |
Returns
| Type | Description |
|---|---|
| string | A formatted string using invariant culture formatting rules. |
InvariantIndexOf(string, string)
Reports the zero-based index of the first occurrence of the specified string using case-insensitive ordinal comparison.
Declaration
public static int InvariantIndexOf(this string s, string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to search within. |
| string | value | The string to search for. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index position of the value if found, or -1 if not found. |
InvariantLastIndexOf(string, string)
Reports the zero-based index of the last occurrence of the specified string using case-insensitive ordinal comparison.
Declaration
public static int InvariantLastIndexOf(this string s, string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to search within. |
| string | value | The string to search for. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index position of the last occurrence of the value if found, or -1 if not found. |
InvariantStartsWith(string, string)
Determines whether the beginning of this string matches the specified string using case-insensitive invariant culture comparison.
Declaration
public static bool InvariantStartsWith(this string compare, string compareTo)
Parameters
| Type | Name | Description |
|---|---|---|
| string | compare | The string to check. |
| string | compareTo | The string to compare to the beginning of this string. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsEmail(string?)
Checks whether a string is a valid email address.
Declaration
public static bool IsEmail(this string? email)
Parameters
| Type | Name | Description |
|---|---|---|
| string | The string to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsFullPath(string)
Checks if a given path is a full path including a drive letter or root.
Declaration
public static bool IsFullPath(this string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The path to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsLowerCase(char)
Determines whether the specified character is lowercase using invariant culture rules.
Declaration
public static bool IsLowerCase(this char ch)
Parameters
| Type | Name | Description |
|---|---|---|
| char | ch | The character to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsNullOrWhiteSpace(string?)
Indicates whether a specified string is null, empty, or consists only of white-space characters.
Declaration
public static bool IsNullOrWhiteSpace(this string? value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The value to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsUpperCase(char)
Determines whether the specified character is uppercase using invariant culture rules.
Declaration
public static bool IsUpperCase(this char ch)
Parameters
| Type | Name | Description |
|---|---|---|
| char | ch | The character to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsVowel(char)
Determines whether the specified character is a vowel (A, E, I, O, U, Y).
Declaration
public static bool IsVowel(this char c)
Parameters
| Type | Name | Description |
|---|---|---|
| char | c | The character to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
MakePluralName(string)
Converts a singular noun to its plural form using common English pluralisation rules.
Declaration
public static string MakePluralName(this string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The singular noun to pluralise. |
Returns
| Type | Description |
|---|---|
| string | The plural form of the noun. |
NormaliseDirectoryPath(string)
Ensures that the folder path ends with a directory separator character.
Declaration
public static string NormaliseDirectoryPath(this string currentFolder)
Parameters
| Type | Name | Description |
|---|---|---|
| string | currentFolder | The folder path to normalise. |
Returns
| Type | Description |
|---|---|
| string | The folder path with a trailing directory separator character. |
NullOrWhiteSpaceAsNull(string?)
Turns a null-or-whitespace string into a null string.
Declaration
public static string? NullOrWhiteSpaceAsNull(this string? text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The string to check. |
Returns
| Type | Description |
|---|---|
| string | Null if the string is null or whitespace; otherwise, the original string. |
OrIfNullOrWhiteSpace(string?, string?)
Returns the alternative value if the string is null or whitespace; otherwise, returns the original string.
Declaration
public static string? OrIfNullOrWhiteSpace(this string? input, string? alternative)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to check. |
| string | alternative | The alternative value to return if the string is null or whitespace. |
Returns
| Type | Description |
|---|---|
| string | The original string if it is not null or whitespace; otherwise, the alternative value. |
ParseInto(string, Type)
Tries to parse a string into the specified type using the type's registered type converter.
Declaration
public static object? ParseInto(this string val, Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| string | val | The string value to parse. |
| Type | type | The type to parse the string into. |
Returns
| Type | Description |
|---|---|
| object | The parsed value, or the original string if it is null or empty. |
ParseInto<T>(string)
Tries to parse a string into the specified type using the type's registered type converter.
Declaration
public static T? ParseInto<T>(this string val)
Parameters
| Type | Name | Description |
|---|---|---|
| string | val | The string value to parse. |
Returns
| Type | Description |
|---|---|
| T | The parsed value, or the original string if it is null or empty. |
Type Parameters
| Name | Description |
|---|---|
| T | The type to parse the string into. |
Replace(string, string, string, StringComparison)
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string, using the specified comparison type.
Declaration
public static string Replace(this string source, string oldString, string newString, StringComparison stringComparison)
Parameters
| Type | Name | Description |
|---|---|---|
| string | source | The current string instance. |
| string | oldString | The string to replace. |
| string | newString | The replacement string. |
| StringComparison | stringComparison | The type of string comparison to use when searching. |
Returns
| Type | Description |
|---|---|
| string | The string with all occurrences replaced. |
ReplaceFirst(string, string, string)
Returns a new string in which only the first occurrence of a specified string is replaced by a specified replacement string.
Declaration
public static string ReplaceFirst(this string text, string search, string replace)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The string to filter. |
| string | search | The string to search for. |
| string | replace | The replacement string. |
Returns
| Type | Description |
|---|---|
| string | The filtered string with the first occurrence replaced. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when text is null. |
ReplaceMany(string, char[], char)
Returns a new string in which all occurrences of specified characters are replaced by a specified character.
Declaration
public static string ReplaceMany(this string text, char[] chars, char replacement)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The string to filter. |
| char[] | chars | The characters to replace. |
| char | replacement | The replacement character. |
Returns
| Type | Description |
|---|---|
| string | The filtered string with all specified characters replaced. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when text or chars is null. |
ReplaceMany(string, IDictionary<string, string>)
Returns a new string in which all occurrences of specified strings are replaced by other specified strings.
Declaration
public static string ReplaceMany(this string text, IDictionary<string, string> replacements)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The string to filter. |
| IDictionary<string, string> | replacements | A dictionary mapping strings to find to their replacement values. |
Returns
| Type | Description |
|---|---|
| string | The filtered string with all replacements applied. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when text or replacements is null. |
ReplaceNonAlphanumericChars(string, char)
Replaces all non-alphanumeric characters in a string with the specified replacement character.
Declaration
public static string ReplaceNonAlphanumericChars(this string input, char replacement)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to process. |
| char | replacement | The character to replace non-alphanumeric characters with. |
Returns
| Type | Description |
|---|---|
| string | The string with all non-alphanumeric characters replaced. |
ReplaceNonAlphanumericChars(string, string)
Replaces all non-alphanumeric characters in a string with the specified replacement string.
Declaration
public static string ReplaceNonAlphanumericChars(this string input, string replacement)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to process. |
| string | replacement | The string to replace non-alphanumeric characters with. |
Returns
| Type | Description |
|---|---|
| string | The string with all non-alphanumeric characters replaced. |
SplitPascalCasing(string, IShortStringHelper)
Splits a Pascal-cased string into a phrase separated by spaces.
Declaration
public static string SplitPascalCasing(this string phrase, IShortStringHelper shortStringHelper)
Parameters
| Type | Name | Description |
|---|---|---|
| string | phrase | The text to split. |
| IShortStringHelper | shortStringHelper | The short string helper. |
Returns
| Type | Description |
|---|---|
| string | The split text with spaces between words. |
StripFileExtension(string)
Strips the file extension from a file name.
Declaration
public static string StripFileExtension(this string fileName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fileName | The file name to process. |
Returns
| Type | Description |
|---|---|
| string | The file name without the extension, or the original file name if no valid extension is found. |
StripHtml(string)
Strips all HTML tags from a string.
Declaration
public static string StripHtml(this string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to strip HTML from. |
Returns
| Type | Description |
|---|---|
| string | The string with all HTML tags removed. |
StripHtml(string, string)
Strips all HTML tags from a string, replacing them with the specified character and ensuring that no more than one instance of the replacement string appears in a row.
Declaration
public static string StripHtml(this string text, string replacement)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to strip HTML from. |
| string | replacement | The string to replace the HTML tags with. |
Returns
| Type | Description |
|---|---|
| string | The string with all HTML tags removed. |
StripNewLines(string)
Strips carriage returns and line feeds from the specified text.
Declaration
public static string StripNewLines(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to process. |
Returns
| Type | Description |
|---|---|
| string | The string with all carriage returns and line feeds removed. |
StripWhitespace(string)
Removes all whitespace characters including new lines, tabs, and spaces.
Declaration
public static string StripWhitespace(this string txt)
Parameters
| Type | Name | Description |
|---|---|---|
| string | txt | The string to strip whitespace from. |
Returns
| Type | Description |
|---|---|
| string | The string with all whitespace removed. |
ToCSharpString(string)
Converts a literal string into a C# string expression with escape sequences.
Declaration
public static string ToCSharpString(this string s)
Parameters
| Type | Name | Description |
|---|---|---|
| string | s | The string to convert. |
Returns
| Type | Description |
|---|---|
| string | The string formatted as a C# string literal, or "<null>" if the input is null. |
ToCleanString(string, IShortStringHelper, CleanStringType)
Cleans a string according to the specified string type.
Declaration
public static string ToCleanString(this string text, IShortStringHelper shortStringHelper, CleanStringType stringType)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to clean. |
| IShortStringHelper | shortStringHelper | The short string helper. |
| CleanStringType | stringType | A flag indicating the target casing and encoding of the string. By default, strings are cleaned up to camelCase and ASCII. |
Returns
| Type | Description |
|---|---|
| string | The cleaned string. |
Remarks
The string is cleaned in the context of the short string helper's default culture.
ToCleanString(string, IShortStringHelper, CleanStringType, char)
Cleans a string according to the specified string type, using a specified separator.
Declaration
public static string ToCleanString(this string text, IShortStringHelper shortStringHelper, CleanStringType stringType, char separator)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to clean. |
| IShortStringHelper | shortStringHelper | The short string helper. |
| CleanStringType | stringType | A flag indicating the target casing and encoding of the string. By default, strings are cleaned up to camelCase and ASCII. |
| char | separator | The separator character to use. |
Returns
| Type | Description |
|---|---|
| string | The cleaned string. |
Remarks
The string is cleaned in the context of the short string helper's default culture.
ToCleanString(string, IShortStringHelper, CleanStringType, char, string)
Cleans a string according to the specified string type, in the context of a specified culture, using a specified separator.
Declaration
public static string ToCleanString(this string text, IShortStringHelper shortStringHelper, CleanStringType stringType, char separator, string culture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to clean. |
| IShortStringHelper | shortStringHelper | The short string helper. |
| CleanStringType | stringType | A flag indicating the target casing and encoding of the string. By default, strings are cleaned up to camelCase and ASCII. |
| char | separator | The separator character to use. |
| string | culture | The culture code. |
Returns
| Type | Description |
|---|---|
| string | The cleaned string. |
ToCleanString(string, IShortStringHelper, CleanStringType, string)
Cleans a string according to the specified string type, in the context of a specified culture.
Declaration
public static string ToCleanString(this string text, IShortStringHelper shortStringHelper, CleanStringType stringType, string culture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to clean. |
| IShortStringHelper | shortStringHelper | The short string helper. |
| CleanStringType | stringType | A flag indicating the target casing and encoding of the string. By default, strings are cleaned up to camelCase and ASCII. |
| string | culture | The culture code. |
Returns
| Type | Description |
|---|---|
| string | The cleaned string. |
ToDelimitedList(string, string)
Splits a delimited string into a list of strings.
Declaration
public static IList<string> ToDelimitedList(this string list, string delimiter = ",")
Parameters
| Type | Name | Description |
|---|---|---|
| string | list | The delimited string to split. |
| string | delimiter | The delimiter used to separate items. Defaults to comma. |
Returns
| Type | Description |
|---|---|
| IList<string> | A list of strings from the delimited input, with empty entries removed and items trimmed. |
ToFirstLower(string)
Returns a copy of the string with the first character converted to lowercase.
Declaration
public static string ToFirstLower(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to convert. |
Returns
| Type | Description |
|---|---|
| string | A string with the first character in lowercase, or the original string if it is null or whitespace. |
ToFirstLower(string, CultureInfo)
Returns a copy of the string with the first character converted to lowercase using the casing rules of the specified culture.
Declaration
public static string ToFirstLower(this string input, CultureInfo culture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to convert. |
| CultureInfo | culture | The culture that provides the casing rules. |
Returns
| Type | Description |
|---|---|
| string | A string with the first character in lowercase, or the original string if it is null or whitespace. |
ToFirstLowerInvariant(string)
Returns a copy of the string with the first character converted to lowercase using the casing rules of the invariant culture.
Declaration
public static string ToFirstLowerInvariant(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to convert. |
Returns
| Type | Description |
|---|---|
| string | A string with the first character in lowercase, or the original string if it is null or whitespace. |
ToFirstUpper(string)
Returns a copy of the string with the first character converted to uppercase.
Declaration
public static string ToFirstUpper(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to convert. |
Returns
| Type | Description |
|---|---|
| string | A string with the first character in uppercase, or the original string if it is null or whitespace. |
ToFirstUpper(string, CultureInfo)
Returns a copy of the string with the first character converted to uppercase using the casing rules of the specified culture.
Declaration
public static string ToFirstUpper(this string input, CultureInfo culture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to convert. |
| CultureInfo | culture | The culture that provides the casing rules. |
Returns
| Type | Description |
|---|---|
| string | A string with the first character in uppercase, or the original string if it is null or whitespace. |
ToFirstUpperInvariant(string)
Returns a copy of the string with the first character converted to uppercase using the casing rules of the invariant culture.
Declaration
public static string ToFirstUpperInvariant(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to convert. |
Returns
| Type | Description |
|---|---|
| string | A string with the first character in uppercase, or the original string if it is null or whitespace. |
ToFriendlyName(string)
Converts a file name to a friendly name suitable for content items.
Declaration
public static string ToFriendlyName(this string fileName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fileName | The file name to convert. |
Returns
| Type | Description |
|---|---|
| string | A friendly name with the extension stripped, underscores and dashes converted to spaces, and title case applied. |
ToGuid(string)
Converts a string to a deterministic GUID using a name-based UUID algorithm.
Declaration
public static Guid ToGuid(this string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to convert to a GUID. |
Returns
| Type | Description |
|---|---|
| Guid | A deterministic GUID derived from the text. |
Remarks
WARNING: Depending on the string, this may not be unique. The same input will always produce the same GUID.
ToInvariantString(int)
Converts an integer to its string representation using the invariant culture.
Declaration
public static string ToInvariantString(this int str)
Parameters
| Type | Name | Description |
|---|---|---|
| int | str | The integer value to convert. |
Returns
| Type | Description |
|---|---|
| string | The string representation of the integer using invariant culture formatting. |
ToInvariantString(long)
Converts a long integer to its string representation using the invariant culture.
Declaration
public static string ToInvariantString(this long str)
Parameters
| Type | Name | Description |
|---|---|---|
| long | str | The long integer value to convert. |
Returns
| Type | Description |
|---|---|
| string | The string representation of the long integer using invariant culture formatting. |
ToSHA1(string)
Converts a string to its SHA1 hash representation.
Declaration
public static string ToSHA1(this string stringToConvert)
Parameters
| Type | Name | Description |
|---|---|---|
| string | stringToConvert | The string to convert. |
Returns
| Type | Description |
|---|---|
| string | The SHA1 hash of the string as a hexadecimal string. |
ToSafeAlias(string, IShortStringHelper?)
Cleans a string to produce a string that can safely be used in an alias.
Declaration
public static string ToSafeAlias(this string alias, IShortStringHelper? shortStringHelper)
Parameters
| Type | Name | Description |
|---|---|---|
| string | alias | The text to filter. |
| IShortStringHelper | shortStringHelper | The short string helper. |
Returns
| Type | Description |
|---|---|
| string | The safe alias. |
ToSafeAlias(string, IShortStringHelper, bool)
Cleans a string to produce a string that can safely be used in an alias.
Declaration
public static string ToSafeAlias(this string alias, IShortStringHelper shortStringHelper, bool camel)
Parameters
| Type | Name | Description |
|---|---|---|
| string | alias | The text to filter. |
| IShortStringHelper | shortStringHelper | The short string helper. |
| bool | camel | A value indicating whether to camel-case the alias. |
Returns
| Type | Description |
|---|---|
| string | The safe alias. |
ToSafeAlias(string, IShortStringHelper, string)
Cleans a string, in the context of a specified culture, to produce a string that can safely be used in an alias.
Declaration
public static string ToSafeAlias(this string alias, IShortStringHelper shortStringHelper, string culture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | alias | The text to filter. |
| IShortStringHelper | shortStringHelper | The short string helper. |
| string | culture | The culture code. |
Returns
| Type | Description |
|---|---|
| string | The safe alias. |
ToSafeFileName(string, IShortStringHelper)
Cleans a string, in the context of the invariant culture, to produce a string that can safely be used as a filename, both internally (on disk) and externally (as a URL).
Declaration
public static string ToSafeFileName(this string text, IShortStringHelper shortStringHelper)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to filter. |
| IShortStringHelper | shortStringHelper | The short string helper. |
Returns
| Type | Description |
|---|---|
| string | The safe filename. |
ToSafeFileName(string, IShortStringHelper, string)
Cleans a string, in the context of a specified culture, to produce a string that can safely be used as a filename, both internally (on disk) and externally (as a URL).
Declaration
public static string ToSafeFileName(this string text, IShortStringHelper shortStringHelper, string culture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to filter. |
| IShortStringHelper | shortStringHelper | The short string helper. |
| string | culture | The culture code. |
Returns
| Type | Description |
|---|---|
| string | The safe filename. |
ToSingleLine(string)
Converts a multi-line string to a single line by replacing line breaks with spaces.
Declaration
public static string ToSingleLine(this string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to convert. |
Returns
| Type | Description |
|---|---|
| string | The text as a single line, or the original text if it is null or empty. |
ToUrlBase64(string)
Encodes a string to a URL-safe base64 string.
Declaration
public static string ToUrlBase64(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The string to encode. |
Returns
| Type | Description |
|---|---|
| string | The URL-safe base64 encoded string. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the input is null. |
ToUrlSegment(string, IShortStringHelper)
Cleans a string to produce a string that can safely be used in a URL segment.
Declaration
public static string ToUrlSegment(this string text, IShortStringHelper shortStringHelper)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to filter. |
| IShortStringHelper | shortStringHelper | The short string helper. |
Returns
| Type | Description |
|---|---|
| string | The safe URL segment. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when text is null. |
| ArgumentException | Thrown when text is empty or consists only of white-space characters. |
ToUrlSegment(string, IShortStringHelper, string?)
Cleans a string, in the context of a specified culture, to produce a string that can safely be used in a URL segment.
Declaration
public static string ToUrlSegment(this string text, IShortStringHelper shortStringHelper, string? culture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to filter. |
| IShortStringHelper | shortStringHelper | The short string helper. |
| string | culture | The culture code. |
Returns
| Type | Description |
|---|---|
| string | The safe URL segment. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when text is null. |
| ArgumentException | Thrown when text is empty or consists only of white-space characters. |
ToValidXmlString(string)
Returns a new string in which all occurrences of Unicode characters that are invalid in XML files are replaced with an empty string.
Declaration
public static string ToValidXmlString(this string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The string to filter. |
Returns
| Type | Description |
|---|---|
| string | The string with invalid XML characters removed. |
Trim(string, string)
Trims the specified string from both the start and end of the value.
Declaration
public static string Trim(this string value, string forRemoving)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The string to trim. |
| string | forRemoving | The string to remove from both ends. |
Returns
| Type | Description |
|---|---|
| string | The trimmed string. |
Remarks
This method accepts a string input whereas the built-in implementation only accepts char or char[].
TrimEnd(string, string)
Trims the specified string from the end of the value.
Declaration
public static string TrimEnd(this string value, string forRemoving)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The string to trim. |
| string | forRemoving | The string to remove from the end. |
Returns
| Type | Description |
|---|---|
| string | The trimmed string. |
TrimStart(string, string)
Trims the specified string from the start of the value.
Declaration
public static string TrimStart(this string value, string forRemoving)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The string to trim. |
| string | forRemoving | The string to remove from the start. |
Returns
| Type | Description |
|---|---|
| string | The trimmed string. |
Truncate(string, int, string)
Truncates a string to the specified maximum length and appends a suffix.
Declaration
public static string Truncate(this string text, int maxLength, string suffix = "...")
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to truncate. |
| int | maxLength | The maximum length of the resulting string including the suffix. |
| string | suffix | The suffix to append when truncation occurs. Defaults to "...". |
Returns
| Type | Description |
|---|---|
| string | The truncated string with suffix, or the original string if it does not exceed the maximum length. |
TruncateWithUniqueHash(string, int)
Truncates a string to the specified maximum length and appends a hash-based suffix to preserve uniqueness.
Declaration
public static string TruncateWithUniqueHash(this string text, int maxLength)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to truncate. |
| int | maxLength | The maximum length of the resulting string including the hash suffix. |
Returns
| Type | Description |
|---|---|
| string | The original string if it does not exceed |
UrlTokenDecode(string)
Decodes a string that was encoded with UrlTokenEncode(byte[]).
Declaration
public static byte[] UrlTokenDecode(this string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The URL token encoded string to decode. |
Returns
| Type | Description |
|---|---|
| byte[] | The decoded byte array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the input is null. |
UrlTokenEncode(byte[])
Encodes a byte array to a URL-safe string by replacing unsafe characters.
Declaration
public static string UrlTokenEncode(this byte[] input)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | input | The byte array to encode. |
Returns
| Type | Description |
|---|---|
| string | The URL-safe encoded string. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the input is null. |