Class StringExtensions
String extension methods
Inheritance
Namespace: Umbraco.Core
Assembly: Umbraco.Core.dll
Syntax
public static class StringExtensions
Methods
View SourceCleanForXss(String, Char[])
Cleans string to aid in preventing xss attacks.
Declaration
public static string CleanForXss(this string input, params char[] ignoreFromClean)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | |
System.Char[] | ignoreFromClean |
Returns
Type | Description |
---|---|
System.String |
ContainsAny(String, IEnumerable<String>, StringComparison)
Checks whether a string "haystack" contains within it any of the strings in the "needles" collection and returns true if it does or false if it doesn't
Declaration
public static bool ContainsAny(this string haystack, IEnumerable<string> needles, StringComparison comparison = StringComparison.CurrentCulture)
Parameters
Type | Name | Description |
---|---|---|
System.String | haystack | The string to check |
System.Collections.Generic.IEnumerable<System.String> | needles | The collection of strings to check are contained within the first string |
System.StringComparison | comparison | The type of comparison to perform - defaults to System.StringComparison.CurrentCulture |
Returns
Type | Description |
---|---|
System.Boolean | True if any of the needles are contained with haystack; otherwise returns false |
ConvertToHex(String)
Converts to hex.
Declaration
public static string ConvertToHex(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The input. |
Returns
Type | Description |
---|---|
System.String |
CsvContains(String, String)
Declaration
public static bool CsvContains(this string csv, string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | csv | |
System.String | value |
Returns
Type | Description |
---|---|
System.Boolean |
DecodeFromHex(String)
Declaration
public static string DecodeFromHex(this string hexValue)
Parameters
Type | Name | Description |
---|---|---|
System.String | hexValue |
Returns
Type | Description |
---|---|
System.String |
DecryptWithMachineKey(String)
Decrypt the encrypted string using the Machine key in medium trust
Declaration
public static string DecryptWithMachineKey(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string value to be decrypted |
Returns
Type | Description |
---|---|
System.String | The decrypted string. |
DetectIsJson(String)
This tries to detect a json string, this is not a fail safe way but it is quicker than doing a try/catch when deserializing when it is not json.
Declaration
public static bool DetectIsJson(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input |
Returns
Type | Description |
---|---|
System.Boolean |
EncodeAsGuid(String)
Encodes as GUID.
Declaration
public static Guid EncodeAsGuid(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The input. |
Returns
Type | Description |
---|---|
System.Guid |
EncodeJsString(String)
Declaration
public static string EncodeJsString(this string s)
Parameters
Type | Name | Description |
---|---|---|
System.String | s |
Returns
Type | Description |
---|---|
System.String |
EncryptWithMachineKey(String)
Encrypt the string using the MachineKey in medium trust
Declaration
public static string EncryptWithMachineKey(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string value to be encrypted. |
Returns
Type | Description |
---|---|
System.String | The encrypted string. |
EnsureEndsWith(String, Char)
Declaration
public static string EnsureEndsWith(this string input, char value)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | |
System.Char | value |
Returns
Type | Description |
---|---|
System.String |
EnsureEndsWith(String, String)
Declaration
public static string EnsureEndsWith(this string input, string toEndWith)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | |
System.String | toEndWith |
Returns
Type | Description |
---|---|
System.String |
EnsureStartsWith(String, Char)
Declaration
public static string EnsureStartsWith(this string input, char value)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | |
System.Char | value |
Returns
Type | Description |
---|---|
System.String |
EnsureStartsWith(String, String)
Declaration
public static string EnsureStartsWith(this string input, string toStartWith)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | |
System.String | toStartWith |
Returns
Type | Description |
---|---|
System.String |
EnumParse<T>(String, Boolean)
Parse string to Enum
Declaration
public static T EnumParse<T>(this string strType, bool ignoreCase)
Parameters
Type | Name | Description |
---|---|---|
System.String | strType | The string to parse |
System.Boolean | ignoreCase | The ignore case |
Returns
Type | Description |
---|---|
T | The parsed enum |
Type Parameters
Name | Description |
---|---|
T | The enum type |
EnumTryParse<T>(String, Boolean, out T)
enum try parse.
Declaration
public static bool EnumTryParse<T>(this string strType, bool ignoreCase, out T result)
Parameters
Type | Name | Description |
---|---|---|
System.String | strType | The str type. |
System.Boolean | ignoreCase | The ignore case. |
T | result | The result. |
Returns
Type | Description |
---|---|
System.Boolean | The enum try parse. |
Type Parameters
Name | Description |
---|---|
T | The type |
EscapedSplit(String, Char, Char)
Splits a string with an escape character that allows for the split character to exist in a string
Declaration
public static IEnumerable<string> EscapedSplit(this string value, char splitChar, char escapeChar = '\\')
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to split |
System.Char | splitChar | The character to split on |
System.Char | escapeChar | The character which can be used to escape the character to split on |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.String> | The string split into substrings delimited by the split character |
EscapeRegexSpecialCharacters(String)
Declaration
public static string EscapeRegexSpecialCharacters(this string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text |
Returns
Type | Description |
---|---|
System.String |
ExceptChars(String, HashSet<Char>)
Declaration
public static string ExceptChars(this string str, HashSet<char> toExclude)
Parameters
Type | Name | Description |
---|---|---|
System.String | str | |
System.Collections.Generic.HashSet<System.Char> | toExclude |
Returns
Type | Description |
---|---|
System.String |
FromUrlBase64(String)
Decodes a URL safe base64 string back
Declaration
public static string FromUrlBase64(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input |
Returns
Type | Description |
---|---|
System.String |
GenerateHash(String)
Generates a hash of a string based on the FIPS compliance setting.
Declaration
public static string GenerateHash(this string str)
Parameters
Type | Name | Description |
---|---|---|
System.String | str | The System.String to hash. |
Returns
Type | Description |
---|---|
System.String | The hashed string. |
GetFileExtension(String)
Determines the extension of the path or URL
Declaration
public static string GetFileExtension(this string file)
Parameters
Type | Name | Description |
---|---|---|
System.String | file |
Returns
Type | Description |
---|---|
System.String | Extension of the file |
IfNullOrWhiteSpace(String, String)
Declaration
public static string IfNullOrWhiteSpace(this string str, string defaultValue)
Parameters
Type | Name | Description |
---|---|---|
System.String | str | |
System.String | defaultValue |
Returns
Type | Description |
---|---|
System.String |
InvariantContains(IEnumerable<String>, String)
Declaration
public static bool InvariantContains(this IEnumerable<string> compare, string compareTo)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.String> | compare | |
System.String | compareTo |
Returns
Type | Description |
---|---|
System.Boolean |
InvariantContains(String, String)
Declaration
public static bool InvariantContains(this string compare, string compareTo)
Parameters
Type | Name | Description |
---|---|---|
System.String | compare | |
System.String | compareTo |
Returns
Type | Description |
---|---|
System.Boolean |
InvariantEndsWith(String, String)
Declaration
public static bool InvariantEndsWith(this string compare, string compareTo)
Parameters
Type | Name | Description |
---|---|---|
System.String | compare | |
System.String | compareTo |
Returns
Type | Description |
---|---|
System.Boolean |
InvariantEquals(String, String)
Compares 2 strings with invariant culture and case ignored
Declaration
public static bool InvariantEquals(this string compare, string compareTo)
Parameters
Type | Name | Description |
---|---|---|
System.String | compare | The compare. |
System.String | compareTo | The compare to. |
Returns
Type | Description |
---|---|
System.Boolean |
InvariantFormat(String, Object[])
formats the string with invariant culture
Declaration
public static string InvariantFormat(this string format, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
System.String | format | The format. |
System.Object[] | args | The args. |
Returns
Type | Description |
---|---|
System.String |
InvariantIndexOf(String, String)
Declaration
public static int InvariantIndexOf(this string s, string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | s | |
System.String | value |
Returns
Type | Description |
---|---|
System.Int32 |
InvariantLastIndexOf(String, String)
Declaration
public static int InvariantLastIndexOf(this string s, string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | s | |
System.String | value |
Returns
Type | Description |
---|---|
System.Int32 |
InvariantStartsWith(String, String)
Declaration
public static bool InvariantStartsWith(this string compare, string compareTo)
Parameters
Type | Name | Description |
---|---|---|
System.String | compare | |
System.String | compareTo |
Returns
Type | Description |
---|---|
System.Boolean |
IsLowerCase(Char)
Declaration
public static bool IsLowerCase(this char ch)
Parameters
Type | Name | Description |
---|---|---|
System.Char | ch |
Returns
Type | Description |
---|---|
System.Boolean |
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 |
---|---|---|
System.String | value | The value to check. |
Returns
Type | Description |
---|---|
System.Boolean | Returns true if the value is null, empty, or consists only of white-space characters, otherwise returns false. |
IsUpperCase(Char)
Declaration
public static bool IsUpperCase(this char ch)
Parameters
Type | Name | Description |
---|---|---|
System.Char | ch |
Returns
Type | Description |
---|---|
System.Boolean |
IsVowel(Char)
Declaration
public static bool IsVowel(this char c)
Parameters
Type | Name | Description |
---|---|---|
System.Char | c |
Returns
Type | Description |
---|---|
System.Boolean |
MakePluralName(String)
Declaration
public static string MakePluralName(this string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name |
Returns
Type | Description |
---|---|
System.String |
NormaliseDirectoryPath(String)
Ensures that the folder path ends with a DirectorySeparatorChar
Declaration
public static string NormaliseDirectoryPath(this string currentFolder)
Parameters
Type | Name | Description |
---|---|---|
System.String | currentFolder |
Returns
Type | Description |
---|---|
System.String |
NullOrWhiteSpaceAsNull(String)
Turns an null-or-whitespace string into a null string.
Declaration
public static string NullOrWhiteSpaceAsNull(this string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text |
Returns
Type | Description |
---|---|
System.String |
OrIfNullOrWhiteSpace(String, String)
Declaration
public static string OrIfNullOrWhiteSpace(this string input, string alternative)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | |
System.String | alternative |
Returns
Type | Description |
---|---|
System.String |
ParseInto(String, Type)
Tries to parse a string into the supplied type by finding and using the Type's "Parse" method
Declaration
public static object ParseInto(this string val, Type type)
Parameters
Type | Name | Description |
---|---|---|
System.String | val | |
System.Type | type |
Returns
Type | Description |
---|---|
System.Object |
ParseInto<T>(String)
Tries to parse a string into the supplied type by finding and using the Type's "Parse" method
Declaration
public static T ParseInto<T>(this string val)
Parameters
Type | Name | Description |
---|---|---|
System.String | val |
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |
Replace(String, String, String, StringComparison)
An extension method that returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string. StringComparison specifies the type of search to use for the specified string.
Declaration
public static string Replace(this string source, string oldString, string newString, StringComparison stringComparison)
Parameters
Type | Name | Description |
---|---|---|
System.String | source | Current instance of the string |
System.String | oldString | Specified string to replace |
System.String | newString | Specified string to inject |
System.StringComparison | stringComparison | String Comparison object to specify search type |
Returns
Type | Description |
---|---|
System.String | Updated string |
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 |
---|---|---|
System.String | text | The string to filter. |
System.Char[] | chars | The characters to replace. |
System.Char | replacement | The replacement character. |
Returns
Type | Description |
---|---|
System.String | The filtered string. |
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 |
---|---|---|
System.String | text | The string to filter. |
System.Collections.Generic.IDictionary<System.String, System.String> | replacements | The replacements definition. |
Returns
Type | Description |
---|---|
System.String | The filtered string. |
SplitPascalCasing(String)
Splits a Pascal cased string into a phrase separated by spaces.
Declaration
public static string SplitPascalCasing(this string phrase)
Parameters
Type | Name | Description |
---|---|---|
System.String | phrase | The text to split. |
Returns
Type | Description |
---|---|
System.String | The split text. |
StripHtml(String)
Strips all HTML from a string.
Declaration
public static string StripHtml(this string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text. |
Returns
Type | Description |
---|---|
System.String | Returns the string without any HTML tags. |
StripNewLines(String)
Strips carrage returns and line feeds from the specified text.
Declaration
public static string StripNewLines(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The input. |
Returns
Type | Description |
---|---|
System.String |
ToCleanString(String, CleanStringType)
Cleans a string.
Declaration
public static string ToCleanString(this string text, CleanStringType stringType)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text to clean. |
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 |
---|---|
System.String | The clean string. |
Remarks
The string is cleaned in the context of the ICurrent.ShortStringHelper default culture.
ToCleanString(String, CleanStringType, Char)
Cleans a string, using a specified separator.
Declaration
public static string ToCleanString(this string text, CleanStringType stringType, char separator)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text to clean. |
CleanStringType | stringType | A flag indicating the target casing and encoding of the string. By default, strings are cleaned up to camelCase and Ascii. |
System.Char | separator | The separator. |
Returns
Type | Description |
---|---|
System.String | The clean string. |
Remarks
The string is cleaned in the context of the ICurrent.ShortStringHelper default culture.
ToCleanString(String, CleanStringType, Char, String)
Cleans a string in the context of a specified culture, using a specified separator.
Declaration
public static string ToCleanString(this string text, CleanStringType stringType, char separator, string culture)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text to clean. |
CleanStringType | stringType | A flag indicating the target casing and encoding of the string. By default, strings are cleaned up to camelCase and Ascii. |
System.Char | separator | The separator. |
System.String | culture | The culture. |
Returns
Type | Description |
---|---|
System.String | The clean string. |
ToCleanString(String, CleanStringType, String)
Cleans a string in the context of a specified culture.
Declaration
public static string ToCleanString(this string text, CleanStringType stringType, string culture)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text to clean. |
CleanStringType | stringType | A flag indicating the target casing and encoding of the string. By default, strings are cleaned up to camelCase and Ascii. |
System.String | culture | The culture. |
Returns
Type | Description |
---|---|
System.String | The clean string. |
ToCSharpString(String)
Converts a literal string into a C# expression.
Declaration
public static string ToCSharpString(this string s)
Parameters
Type | Name | Description |
---|---|---|
System.String | s | Current instance of the string. |
Returns
Type | Description |
---|---|
System.String | The string in a C# format. |
ToDelimitedList(String, String)
The to delimited list.
Declaration
public static IList<string> ToDelimitedList(this string list, string delimiter = ",")
Parameters
Type | Name | Description |
---|---|---|
System.String | list | The list. |
System.String | delimiter | The delimiter. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IList<System.String> | the list |
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 |
---|---|---|
System.String | input | The string. |
Returns
Type | Description |
---|---|
System.String | The converted string. |
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 |
---|---|---|
System.String | input | The string. |
System.Globalization.CultureInfo | culture | The culture. |
Returns
Type | Description |
---|---|
System.String | The converted string. |
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 |
---|---|---|
System.String | input | The string. |
Returns
Type | Description |
---|---|
System.String | The converted string. |
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 |
---|---|---|
System.String | input | The string. |
Returns
Type | Description |
---|---|
System.String | The converted string. |
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 |
---|---|---|
System.String | input | The string. |
System.Globalization.CultureInfo | culture | The culture. |
Returns
Type | Description |
---|---|
System.String | The converted string. |
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 |
---|---|---|
System.String | input | The string. |
Returns
Type | Description |
---|---|
System.String | The converted string. |
ToFriendlyName(String)
Converts a file name to a friendly name for a content item
Declaration
public static string ToFriendlyName(this string fileName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fileName |
Returns
Type | Description |
---|---|
System.String |
ToInvariantString(Int32)
Converts an integer to an invariant formatted string
Declaration
public static string ToInvariantString(this int str)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | str |
Returns
Type | Description |
---|---|
System.String |
ToInvariantString(Int64)
Declaration
public static string ToInvariantString(this long str)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | str |
Returns
Type | Description |
---|---|
System.String |
ToSafeAlias(String)
Cleans a string to produce a string that can safely be used in an alias.
Declaration
public static string ToSafeAlias(this string alias)
Parameters
Type | Name | Description |
---|---|---|
System.String | alias | The text to filter. |
Returns
Type | Description |
---|---|
System.String | The safe alias. |
ToSafeAlias(String, Boolean)
Cleans a string to produce a string that can safely be used in an alias.
Declaration
public static string ToSafeAlias(this string alias, bool camel)
Parameters
Type | Name | Description |
---|---|---|
System.String | alias | The text to filter. |
System.Boolean | camel | A value indicating that we want to camel-case the alias. |
Returns
Type | Description |
---|---|
System.String | The safe alias. |
ToSafeAlias(String, 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, string culture)
Parameters
Type | Name | Description |
---|---|---|
System.String | alias | The text to filter. |
System.String | culture | The culture. |
Returns
Type | Description |
---|---|
System.String | The safe alias. |
ToSafeFileName(String)
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)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text to filter. |
Returns
Type | Description |
---|---|
System.String | The safe filename. |
ToSafeFileName(String, String)
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, string culture)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text to filter. |
System.String | culture | The culture. |
Returns
Type | Description |
---|---|
System.String | The safe filename. |
ToSingleLine(String)
Converts to single line by replacing line breaks with spaces.
Declaration
public static string ToSingleLine(this string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text |
Returns
Type | Description |
---|---|
System.String |
ToUrlBase64(String)
Encodes a string to a safe URL base64 string
Declaration
public static string ToUrlBase64(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input |
Returns
Type | Description |
---|---|
System.String |
ToUrlSegment(String)
Cleans a string to produce a string that can safely be used in an URL segment.
Declaration
public static string ToUrlSegment(this string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text to filter. |
Returns
Type | Description |
---|---|
System.String | The safe URL segment. |
ToUrlSegment(String, String)
Cleans a string, in the context of a specified culture, to produce a string that can safely be used in an URL segment.
Declaration
public static string ToUrlSegment(this string text, string culture)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text to filter. |
System.String | culture | The culture. |
Returns
Type | Description |
---|---|
System.String | The safe URL segment. |
Trim(String, String)
Trims the specified value from a string; accepts a string input whereas the in-built implementation only accepts char or char[].
Declaration
public static string Trim(this string value, string forRemoving)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The value. |
System.String | forRemoving | For removing. |
Returns
Type | Description |
---|---|
System.String |
TrimEnd(String, String)
Declaration
public static string TrimEnd(this string value, string forRemoving)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | |
System.String | forRemoving |
Returns
Type | Description |
---|---|
System.String |
TrimStart(String, String)
Declaration
public static string TrimStart(this string value, string forRemoving)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | |
System.String | forRemoving |
Returns
Type | Description |
---|---|
System.String |
Truncate(String, Int32, String)
Truncates the specified text string.
Declaration
public static string Truncate(this string text, int maxLength, string suffix = "...")
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text. |
System.Int32 | maxLength | Length of the max. |
System.String | suffix | The suffix. |
Returns
Type | Description |
---|---|
System.String |