Class StringExtensions
- Namespace
- Cuemon.Extensions
- Assembly
- Cuemon.Extensions.Core.dll
Extension methods for the string class.
public static class StringExtensions
- Inheritance
-
StringExtensions
Methods
Chunk(string)
Returns a sequence that is chunked into string-slices having a length of 1024 that is equivalent to value.
public static IEnumerable<string> Chunk(this string value)
Parameters
valuestringA string to chunk into a sequence of smaller string-slices for partitioned storage or similar.
Returns
- IEnumerable<string>
A sequence that is chunked into string-slices having a length of 1024 that is equivalent to
value.
Exceptions
- ArgumentNullException
valueis null.
Chunk(string, int)
Returns a sequence that is chunked into string-slices of the specified length that is equivalent to value. Default is 1024.
public static IEnumerable<string> Chunk(this string value, int length)
Parameters
valuestringA string to chunk into a sequence of smaller string-slices for partitioned storage or similar.
lengthintThe desired length of each string-slice in the sequence.
Returns
- IEnumerable<string>
A sequence that is chunked into string-slices of the specified
lengththat is equivalent tovalue.
Exceptions
- ArgumentNullException
valueis null.- ArgumentOutOfRangeException
lengthis less or equal to 0.
ContainsAll(string, StringComparison, params string[])
Returns a value indicating whether all of the specified values occurs within the value.
public static bool ContainsAll(this string value, StringComparison comparison, params string[] values)
Parameters
valuestringThe string to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
valuesstring[]The string sequence to search within
value.
Returns
- bool
trueif all of thevaluesoccurs within thevalue; otherwise,false.
Exceptions
- ArgumentNullException
valueis null -or-valuesis null.
ContainsAll(string, params string[])
Returns a value indicating whether all of the specified values occurs within the value.
public static bool ContainsAll(this string value, params string[] values)
Parameters
Returns
- bool
trueif all of thevaluesoccurs within thevalue; otherwise,false.
Remarks
This method performs an ordinal (case-insensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.
ContainsAny(string, char, StringComparison)
Returns a value indicating whether the specified find occurs within the value.
public static bool ContainsAny(this string value, char find, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
Parameters
valuestringThe string to extend.
findcharThe char to search within
value.comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison. Default is OrdinalIgnoreCase.
Returns
- bool
trueif thefindparameter occurs within thevalue; otherwise,false.
Exceptions
- ArgumentNullException
valueis null -or-findis null.
ContainsAny(string, params char[])
Returns a value indicating whether the specified values occurs within the value object.
public static bool ContainsAny(this string value, params char[] values)
Parameters
Returns
- bool
trueif thevaluesparameter occurs within thevalue; otherwise,false.
Remarks
This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.
ContainsAny(string, string, StringComparison)
Returns a value indicating whether the specified find occurs within the value.
public static bool ContainsAny(this string value, string find, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
Parameters
valuestringThe string to extend.
findstringThe string to find within
value.comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison. Default is OrdinalIgnoreCase.
Returns
- bool
trueif thefindparameter occurs within thevalue; otherwise,false.
Exceptions
- ArgumentNullException
valueis null -or-findis null.
ContainsAny(string, StringComparison, params char[])
Returns a value indicating whether the specified values occurs within the value object.
public static bool ContainsAny(this string value, StringComparison comparison, params char[] values)
Parameters
valuestringThe string to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
valueschar[]The char sequence to search within
value.
Returns
- bool
trueif thevaluesparameter occurs within thevalue; otherwise,false.
Exceptions
- ArgumentNullException
valueis null -or-valuesis null.
ContainsAny(string, StringComparison, params string[])
Returns a value indicating whether any of the specified values occurs within the value.
public static bool ContainsAny(this string value, StringComparison comparison, params string[] values)
Parameters
valuestringThe string to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
valuesstring[]The string sequence to search within
value.
Returns
- bool
trueif any of thevaluesoccurs within thevalue; otherwise,false.
Exceptions
- ArgumentNullException
valueis null -or-valuesis null.
ContainsAny(string, params string[])
Returns a value indicating whether any of the specified values occurs within the value.
public static bool ContainsAny(this string value, params string[] values)
Parameters
Returns
- bool
trueif any of thevaluesoccurs within thevalue; otherwise,false.
Remarks
This method performs an ordinal (case-insensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.
Count(string, char)
Counts the occurrences of character in the specified value.
public static int Count(this string value, char character)
Parameters
Returns
- int
The number of times the
characterwas found in thevalue.
Exceptions
- ArgumentNullException
valueis null.
Difference(string, string)
Returns the set difference between second and first or Empty if no difference.
public static string Difference(this string first, string second)
Parameters
firststringThe value where characters that are not also in
secondwill be returned.secondstringThe value to compare with
first.
Returns
- string
A string that contains the set difference between
secondandfirstor Empty if no difference.
EqualsAny(string, StringComparison, params string[])
Returns a value indicating the specified value equals one of the specified values.
public static bool EqualsAny(this string value, StringComparison comparison, params string[] values)
Parameters
valuestringThe string to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
valuesstring[]The string sequence to search within
value.
Returns
- bool
trueif one thevaluesis the same as thevalue; otherwisefalse.
EqualsAny(string, params string[])
Returns a value indicating the specified value equals one of the specified values.
public static bool EqualsAny(this string value, params string[] values)
Parameters
Returns
- bool
trueif one thevaluesis the same as thevalue; otherwisefalse.
Remarks
This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.
Exceptions
- ArgumentNullException
valueis null - or -valuesis null.
FromBase64(string)
Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array.
public static byte[] FromBase64(this string value)
Parameters
Returns
- byte[]
An array of 8-bit unsigned integers that is equivalent to
value.
FromBinaryDigits(string)
Converts the specified input of binary digits to its equivalent byte[] representation.
public static byte[] FromBinaryDigits(this string input)
Parameters
Returns
- byte[]
A byte[] that is equivalent to
input.
Exceptions
- ArgumentNullException
inputcannot be null.- ArgumentException
inputcannot be empty or consist only of white-space characters.- FormatException
inputmust consist only of binary digits.
- See Also
FromHexadecimal(string, Action<EncodingOptions>)
Converts the specified hexadecimal value to its equivalent string representation.
public static string FromHexadecimal(this string value, Action<EncodingOptions> setup = null)
Parameters
valuestringThe string to extend.
setupAction<EncodingOptions>The EncodingOptions which need to be configured.
Returns
Remarks
EncodingOptions will be initialized with DefaultPreambleSequence and DefaultEncoding.
Exceptions
- ArgumentNullException
valuecannot be null.- ArgumentException
valuemust be hexadecimal.
- See Also
FromUrlEncodedBase64(string)
Converts the specified input of URL-safe base64 characters to its equivalent byte[] representation.
public static byte[] FromUrlEncodedBase64(this string input)
Parameters
Returns
- byte[]
A byte[] that is equivalent to
input.
Exceptions
- ArgumentNullException
inputcannot be null.- ArgumentException
inputcannot be empty or consist only of white-space characters.- ArgumentOutOfRangeException
inputhas illegal base64 characters.
- See Also
IsBase64(string)
Determines whether the specified value matches a Base64 structure.
public static bool IsBase64(this string value)
Parameters
Returns
- bool
trueif the specifiedvaluematches a Base64 structure; otherwise,false.
Remarks
This method will skip common Base64 structures typically used as checksums. This includes 32, 128, 160, 256, 384 and 512 bit checksums.
IsEmailAddress(string)
Determines whether the specified value has a valid format of an email address.
public static bool IsEmailAddress(this string value)
Parameters
Returns
- bool
trueif the specifiedvaluehas a valid format of an email address; otherwise,false.
IsGuid(string, GuidFormats)
Determines whether the specified value has a valid format of a Guid.
public static bool IsGuid(this string value, GuidFormats format = GuidFormats.D | GuidFormats.B | GuidFormats.P)
Parameters
valuestringThe string to extend.
formatGuidFormatsA bitmask comprised of one or more GuidFormats that specify how the GUID parsing is conducted.
Returns
IsHex(string)
Determines whether the specified value is hexadecimal.
public static bool IsHex(this string value)
Parameters
Returns
- bool
trueif the specifiedvalueis hexadecimal; otherwise,false.
IsNullOrEmpty(IEnumerable<string>)
Determines whether a string sequence has at least one value that equals to null or empty.
public static bool IsNullOrEmpty(this IEnumerable<string> source)
Parameters
sourceIEnumerable<string>The string sequence to extend.
Returns
- bool
trueif a string sequence has at least one value that equals to null or empty; otherwise,false.
Exceptions
- ArgumentNullException
sourceis null.
IsNullOrEmpty(string)
Determines whether the specified value is null or an Empty string.
public static bool IsNullOrEmpty(this string value)
Parameters
Returns
- bool
trueif thevalueis null or an empty string (""); otherwise,false.
IsNullOrWhiteSpace(string)
Determines whether the specified value is null, empty, or consists only of white-space characters.
public static bool IsNullOrWhiteSpace(this string value)
Parameters
Returns
- bool
trueif the value parameter is null or an empty string (""), or if value consists exclusively of white-space characters; otherwise,false.
IsNumeric(string, NumberStyles, IFormatProvider)
Determines whether the specified value can be evaluated as a number.
public static bool IsNumeric(this string value, NumberStyles style = NumberStyles.Number, IFormatProvider provider = null)
Parameters
valuestringThe string to extend.
styleNumberStylesA bitwise combination of NumberStyles values that indicates the permitted format of
value.providerIFormatProviderAn IFormatProvider that supplies culture-specific formatting information about
value.
Returns
- bool
trueif the specified value can be evaluated as a number; otherwise,false.
IsSequenceOf<T>(IEnumerable<string>, CultureInfo, ITypeDescriptorContext, Func<string, CultureInfo, bool>)
Determines whether the elements of the specified source is equivalent to the specified T.
public static bool IsSequenceOf<T>(this IEnumerable<string> source, CultureInfo culture = null, ITypeDescriptorContext context = null, Func<string, CultureInfo, bool> parser = null)
Parameters
sourceIEnumerable<string>The string sequence to extend.
cultureCultureInfoThe culture-specific formatting information to apply on the elements within
source.contextITypeDescriptorContextThe type-specific formatting information to apply on the elements within
source.parserFunc<string, CultureInfo, bool>The function delegate that evaluates if the elements of
sourceis equivalent to the specifiedT.
Returns
- bool
trueif elements of thesourceparameter was successfully converted; otherwisefalse.
Type Parameters
TThe type of the expected values contained within the sequence of
source.
Exceptions
- ArgumentNullException
sourcecannot be null.
JsEscape(string)
Escapes the given string the same way as the well known JavaScript escape() function.
public static string JsEscape(this string value)
Parameters
Returns
- string
The input
valuewith an escaped equivalent.
JsUnescape(string)
Unescapes the given string the same way as the well known Javascript unescape() function.
public static string JsUnescape(this string value)
Parameters
Returns
- string
The input
valuewith an unescaped equivalent.
PrefixWith(string, string)
Prefixes the source with the specified value.
public static string PrefixWith(this string source, string value)
Parameters
Returns
- string
A string with a prefix
value.
RemoveAll(string, params char[])
Returns a new string in which all the specified filter values has been deleted from the specified value.
public static string RemoveAll(this string value, params char[] filter)
Parameters
valuestringThe string to extend.
filterchar[]The filter containing the characters and/or words to delete.
Returns
- string
A new string that is equivalent to
valueexcept for the removed characters.
RemoveAll(string, StringComparison, params string[])
Returns a new string in which all the specified filter values has been deleted from the specified value.
public static string RemoveAll(this string value, StringComparison comparison, params string[] filter)
Parameters
valuestringThe string to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
filterstring[]The filter containing the characters and/or words to delete.
Returns
- string
A new string that is equivalent to
valueexcept for the removed characters and/or words.
Exceptions
- ArgumentNullException
valueis null orfilteris null.
RemoveAll(string, params string[])
Returns a new string in which all the specified filter values has been deleted from the specified value.
public static string RemoveAll(this string value, params string[] filter)
Parameters
valuestringThe string to extend.
filterstring[]The filter containing the characters and/or words to delete.
Returns
- string
A new string that is equivalent to
valueexcept for the removed characters and/or words.
Remarks
This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.
Exceptions
- ArgumentNullException
valueis null orfilteris null.
RemoveAll(string[], StringComparison, params string[])
Returns a new string array in which all the specified filter values has been deleted from the specified source array.
public static string[] RemoveAll(this string[] source, StringComparison comparison, params string[] filter)
Parameters
sourcestring[]The string sequence to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
filterstring[]The filter containing the characters and/or words to delete.
Returns
- string[]
A new string array that is equivalent to
sourceexcept for the removed characters and/or words.
Exceptions
- ArgumentNullException
sourceis null orfilteris null.
RemoveAll(string[], params string[])
Returns a new string array in which all the specified filter values has been deleted from the specified source array.
public static string[] RemoveAll(this string[] source, params string[] filter)
Parameters
sourcestring[]The string sequence to extend.
filterstring[]The filter containing the characters and/or words to delete.
Returns
- string[]
A new string array that is equivalent to
sourceexcept for the removed characters and/or words.
Remarks
This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.
Exceptions
- ArgumentNullException
sourceis null orfilteris null.
ReplaceAll(string, string, string, StringComparison)
Replaces all occurrences of oldValue in value, with newValue.
public static string ReplaceAll(this string value, string oldValue, string newValue, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
Parameters
valuestringThe string to extend.
oldValuestringThe string value to be replaced.
newValuestringThe string value to replace all occurrences of
oldValue.comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison. Default is OrdinalIgnoreCase.
Returns
SplitDelimited(string, Action<DelimitedStringOptions>)
Returns a string[] that contain the substrings of value delimited by a Delimiter that may be quoted by Qualifier.
public static string[] SplitDelimited(this string value, Action<DelimitedStringOptions> setup = null)
Parameters
valuestringThe string to extend.
setupAction<DelimitedStringOptions>The DelimitedStringOptions which may be configured.
Returns
- string[]
A string[] that contain the substrings of
valuedelimited by a Delimiter and optionally surrounded within Qualifier.
Remarks
This method was inspired by two articles on StackOverflow @ http://stackoverflow.com/questions/2807536/split-string-in-c-sharp and https://stackoverflow.com/questions/3776458/split-a-comma-separated-string-with-both-quoted-and-unquoted-strings. The default implementation conforms with the RFC-4180 standard.
Exceptions
- InvalidOperationException
An error occurred while splitting
valueinto substrings separated by Delimiter and quoted with Qualifier. This is typically related to data corruption, eg. a field has not been properly closed with the Qualifier specified.
StartsWith(string, IEnumerable<string>)
Determines whether the beginning of an instance of string matches at least one string in the specified sequence of strings.
public static bool StartsWith(this string value, IEnumerable<string> startWithValues)
Parameters
valuestringThe string to extend.
startWithValuesIEnumerable<string>A sequence of string values to match against.
Returns
- bool
trueif at least one value matches the beginning of this string; otherwise,false.
Remarks
This match is performed by using a default value of OrdinalIgnoreCase.
StartsWith(string, StringComparison, IEnumerable<string>)
Determines whether the beginning of an instance of string matches at least one string in the specified sequence of strings.
public static bool StartsWith(this string value, StringComparison comparison, IEnumerable<string> strings)
Parameters
valuestringThe string to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
stringsIEnumerable<string>A sequence of string values to match against.
Returns
- bool
trueif at least one value matches the beginning of this string; otherwise,false.
StartsWith(string, StringComparison, params string[])
Determines whether the beginning of an instance of string matches at least one string in the specified sequence of strings.
public static bool StartsWith(this string value, StringComparison comparison, params string[] strings)
Parameters
valuestringThe string to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
stringsstring[]A sequence of string values to match against.
Returns
- bool
trueif at least one value matches the beginning of this string; otherwise,false.
Remarks
This match is performed by using a default value of OrdinalIgnoreCase.
StartsWith(string, params string[])
Determines whether the beginning of an instance of string matches at least one string in the specified sequence of strings.
public static bool StartsWith(this string value, params string[] strings)
Parameters
Returns
- bool
trueif at least one value matches the beginning of this string; otherwise,false.
Remarks
This match is performed by using a default value of OrdinalIgnoreCase.
SubstringBefore(string, string, StringComparison)
Retrieves a substring from the specified value. The substring starts at position 0 and continues until the first occurrence of match.
public static string SubstringBefore(this string value, string match, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
Parameters
valuestringThe string to extend.
matchstringThe match that will define the stopping point.
comparisonTypeStringComparisonOne of the enumeration values that specifies the rules for the search.
Returns
- string
A substring that contains only the value just before
match.
SuffixWith(string, string)
Suffixes the source with the specified value.
public static string SuffixWith(this string source, string value)
Parameters
Returns
- string
A string with a suffix
value.
SuffixWithForwardingSlash(string)
Suffixes the source with a forwarding slash.
public static string SuffixWithForwardingSlash(this string source)
Parameters
sourcestringThe string to extend.
Returns
- string
A string with a suffix forwarding slash.
ToByteArray(string, Action<EncodingOptions>)
Converts the specified input to its equivalent byte[] representation.
public static byte[] ToByteArray(this string input, Action<EncodingOptions> setup = null)
Parameters
inputstringThe string to be converted into a byte[].
setupAction<EncodingOptions>The EncodingOptions which may be configured.
Returns
- byte[]
A byte[] that is equivalent to
input.
Exceptions
- ArgumentNullException
inputcannot be null.- InvalidEnumArgumentException
setupwas initialized with an invalid Preamble.
- See Also
ToCasing(string, CasingMethod)
Converts the specified value to either lowercase, UPPERCASE, Title Case or unaltered.
public static string ToCasing(this string value, CasingMethod method = CasingMethod.Default)
Parameters
valuestringThe string to extend.
methodCasingMethodThe method to use in the conversion.
Returns
Remarks
Uses InvariantCulture for the conversion.
ToCasing(string, CasingMethod, CultureInfo)
Converts the specified value to either lowercase, UPPERCASE, Title Case or unaltered using the specified culture.
public static string ToCasing(this string value, CasingMethod method, CultureInfo culture)
Parameters
valuestringThe string to extend.
methodCasingMethodThe method to use in the conversion.
cultureCultureInfoThe culture rules to apply the conversion.
Returns
Exceptions
- ArgumentNullException
valuecannot be null -or-culturecannot be null.
ToEnum<TEnum>(string, bool)
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated TEnum.
public static TEnum ToEnum<TEnum>(this string value, bool ignoreCase = true) where TEnum : struct, IConvertible
Parameters
Returns
- TEnum
An enum of type
TEnumwhose value is represented byvalue.
Type Parameters
TEnumThe type of the enumeration to convert.
Exceptions
- ArgumentNullException
valueis null.- ArgumentException
TEnumdoes not represents an enumeration.- ArgumentException
valuedoes not represents an enumeration.
- See Also
-
FromEnum()
ToGuid(string, Action<GuidStringOptions>)
Converts the specified input of a GUID to its equivalent Guid structure.
public static Guid ToGuid(this string input, Action<GuidStringOptions> setup = null)
Parameters
inputstringThe string to extend.
setupAction<GuidStringOptions>The GuidStringOptions which may be configured.
Returns
Exceptions
- ArgumentNullException
inputcannot be null.- ArgumentException
inputcannot be empty or consist only of white-space characters.- FormatException
The specified
inputwas not recognized to be a GUID.
- See Also
-
FromGuid()
ToHexadecimal(string, Action<EncodingOptions>)
Converts the specified value to its equivalent hexadecimal representation.
public static string ToHexadecimal(this string value, Action<EncodingOptions> setup = null)
Parameters
valuestringThe string to extend.
setupAction<EncodingOptions>The EncodingOptions which need to be configured.
Returns
Remarks
EncodingOptions will be initialized with DefaultPreambleSequence and DefaultEncoding.
ToTimeSpan(string, TimeUnit)
Converts the specified value to its equivalent TimeSpan representation.
public static TimeSpan ToTimeSpan(this string value, TimeUnit timeUnit)
Parameters
valuestringThe string to extend.
timeUnitTimeUnitOne of the enumeration values that specifies the outcome of the conversion.
Returns
Exceptions
- ArgumentNullException
valueis null.- OverflowException
The
valuepaired withtimeUnitis outside its valid range.- ArgumentOutOfRangeException
timeUnitwas outside its valid range.
ToUri(string, UriKind)
Converts the specified value to its equivalent Uri representation.
public static Uri ToUri(this string value, UriKind uriKind = UriKind.Absolute)
Parameters
valuestringThe string to extend.
uriKindUriKindSpecifies whether the URI string is a relative URI, absolute URI, or is indeterminate.
Returns
Exceptions
- ArgumentNullException
valuecannot be null.- ArgumentException
valuecannot be empty or consist only of white-space characters.
TrimAll(string)
Removes all occurrences of white-space characters from the specified value.
public static string TrimAll(this string value)
Parameters
Returns
- string
The string that remains after all occurrences of white-space characters are removed from the specified
value.
Exceptions
- ArgumentNullException
valueis null.
TrimAll(string, params char[])
Removes all occurrences of a set of characters specified in trimChars from the specified value.
public static string TrimAll(this string value, params char[] trimChars)
Parameters
valuestringThe string to extend.
trimCharschar[]An array of Unicode characters to remove. Default value is WhiteSpace.
Returns
- string
The string that remains after all occurrences of the characters in the
trimCharsparameter are removed from the specifiedvalue.
Exceptions
- ArgumentNullException
valueis null.