Table of Contents

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

value String

A 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

value is null.

Chunk(String, Int32)

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

value String

A String to chunk into a sequence of smaller string-slices for partitioned storage or similar.

length Int32

The desired length of each string-slice in the sequence.

Returns

IEnumerable<String>

A sequence that is chunked into string-slices of the specified length that is equivalent to value.

Exceptions

ArgumentNullException

value is null.

ArgumentOutOfRangeException

length is less or equal to 0.

ContainsAll(String, 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

value String

The String to extend.

values String[]

The String sequence to search within value.

Returns

Boolean

true if all of the values occurs within the value; 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.

ContainsAll(String, StringComparison, 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

value String

The String to extend.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

values String[]

The String sequence to search within value.

Returns

Boolean

true if all of the values occurs within the value; otherwise, false.

Exceptions

ArgumentNullException

value is null -or- values is null.

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

value String

The String to extend.

find Char

The Char to search within value.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison. Default is OrdinalIgnoreCase.

Returns

Boolean

true if the find parameter occurs within the value; otherwise, false.

Exceptions

ArgumentNullException

value is null -or- find is null.

ContainsAny(String, 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

value String

The String to extend.

values Char[]

The Char sequence to search within value.

Returns

Boolean

true if the values parameter occurs within the value; 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

value String

The String to extend.

find String

The String to find within value.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison. Default is OrdinalIgnoreCase.

Returns

Boolean

true if the find parameter occurs within the value; otherwise, false.

Exceptions

ArgumentNullException

value is null -or- find is null.

ContainsAny(String, 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

value String

The String to extend.

values String[]

The String sequence to search within value.

Returns

Boolean

true if any of the values occurs within the value; 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, StringComparison, 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

value String

The String to extend.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

values Char[]

The Char sequence to search within value.

Returns

Boolean

true if the values parameter occurs within the value; otherwise, false.

Exceptions

ArgumentNullException

value is null -or- values is null.

ContainsAny(String, StringComparison, 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

value String

The String to extend.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

values String[]

The String sequence to search within value.

Returns

Boolean

true if any of the values occurs within the value; otherwise, false.

Exceptions

ArgumentNullException

value is null -or- values is null.

Count(String, Char)

Counts the occurrences of character in the specified value.

public static int Count(this string value, char character)

Parameters

value String

The String to extend.

character Char

The Char value to count in value.

Returns

Int32

The number of times the character was found in the value.

Exceptions

ArgumentNullException

value is 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

first String

The value where characters that are not also in second will be returned.

second String

The value to compare with first.

Returns

String

A String that contains the set difference between second and first or Empty if no difference.

EqualsAny(String, 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

value String

The String to extend.

values String[]

The String sequence to search within value.

Returns

Boolean

true if one the values is the same as the value; 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.

Exceptions

ArgumentNullException

value is null - or - values is null.

EqualsAny(String, StringComparison, 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

value String

The String to extend.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

values String[]

The String sequence to search within value.

Returns

Boolean

true if one the values is the same as the value; otherwise false.

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

value String

The String to extend.

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

input String

The String to extend.

Returns

Byte[]

A byte[] that is equivalent to input.

Exceptions

ArgumentNullException

input cannot be null.

ArgumentException

input cannot be empty or consist only of white-space characters.

FormatException

input must 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

value String

The String to extend.

setup Action<EncodingOptions>

The EncodingOptions which need to be configured.

Returns

String

A String representation of the hexadecimal characters in value.

Remarks

Exceptions

ArgumentNullException

value cannot be null.

ArgumentException

value must 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

input String

The String to extend.

Returns

Byte[]

A byte[] that is equivalent to input.

Exceptions

ArgumentNullException

input cannot be null.

ArgumentException

input cannot be empty or consist only of white-space characters.

ArgumentOutOfRangeException

input has illegal base64 characters.

See Also

IsBase64(String)

Determines whether the specified value matches a Base64 structure.

public static bool IsBase64(this string value)

Parameters

value String

The String to extend.

Returns

Boolean

true if the specified value matches 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

value String

The String to extend.

Returns

Boolean

true if the specified value has 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

value String

The String to extend.

format GuidFormats

A bitmask comprised of one or more GuidFormats that specify how the GUID parsing is conducted.

Returns

Boolean

true if the specified value has a format of a Guid; otherwise, false.

IsHex(String)

Determines whether the specified value is hexadecimal.

public static bool IsHex(this string value)

Parameters

value String

The String to extend.

Returns

Boolean

true if the specified value is 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

source IEnumerable<String>

The String sequence to extend.

Returns

Boolean

true if a string sequence has at least one value that equals to null or empty; otherwise, false.

Exceptions

ArgumentNullException

source is null.

IsNullOrEmpty(String)

Determines whether the specified value is null or an Empty string.

public static bool IsNullOrEmpty(this string value)

Parameters

value String

The String to extend.

Returns

Boolean

true if the value is 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

value String

The String to extend.

Returns

Boolean

true if 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

value String

The String to extend.

style NumberStyles

A bitwise combination of NumberStyles values that indicates the permitted format of value.

provider IFormatProvider

An IFormatProvider that supplies culture-specific formatting information about value.

Returns

Boolean

true if the specified value can be evaluated as a number; otherwise, false.

IsSequenceOf<T>(IEnumerable<String>, CultureInfo, ITypeDescriptorContext, Func<String, CultureInfo, Boolean>)

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

source IEnumerable<String>

The String sequence to extend.

culture CultureInfo

The culture-specific formatting information to apply on the elements within source.

context ITypeDescriptorContext

The type-specific formatting information to apply on the elements within source.

parser Func<String, CultureInfo, Boolean>

The function delegate that evaluates if the elements of source is equivalent to the specified T.

Returns

Boolean

true if elements of the source parameter was successfully converted; otherwise false.

Type Parameters

T

The type of the expected values contained within the sequence of source.

Exceptions

ArgumentNullException

source cannot 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

value String

The String to extend.

Returns

String

The input value with 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

value String

The String to extend.

Returns

String

The input value with an unescaped equivalent.

PrefixWith(String, String)

Prefixes the source with the specified value.

public static string PrefixWith(this string source, string value)

Parameters

source String

The string to extend.

value String

The value to prefix source.

Returns

String

A string with a prefix value.

RemoveAll(String, 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

value String

The String to extend.

filter Char[]

The filter containing the characters and/or words to delete.

Returns

String

A new string that is equivalent to value except for the removed characters.

RemoveAll(String, 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

value String

The String to extend.

filter String[]

The filter containing the characters and/or words to delete.

Returns

String

A new string that is equivalent to value except 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

value is null or filter is null.

RemoveAll(String, StringComparison, 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

value String

The String to extend.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

filter String[]

The filter containing the characters and/or words to delete.

Returns

String

A new string that is equivalent to value except for the removed characters and/or words.

Exceptions

ArgumentNullException

value is null or filter is null.

RemoveAll(String[], 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

source String[]

The String sequence to extend.

filter String[]

The filter containing the characters and/or words to delete.

Returns

String[]

A new string array that is equivalent to source except 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

source is null or filter is null.

RemoveAll(String[], StringComparison, 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

source String[]

The String sequence to extend.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

filter String[]

The filter containing the characters and/or words to delete.

Returns

String[]

A new string array that is equivalent to source except for the removed characters and/or words.

Exceptions

ArgumentNullException

source is null or filter is 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

value String

The String to extend.

oldValue String

The String value to be replaced.

newValue String

The String value to replace all occurrences of oldValue.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison. Default is OrdinalIgnoreCase.

Returns

String

A String equivalent to value but with all instances of oldValue replaced with newValue.

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

value String

The String to extend.

setup Action<DelimitedStringOptions>

The DelimitedStringOptions which may be configured.

Returns

String[]

A string[] that contain the substrings of value delimited 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 value into 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

value String

The String to extend.

startWithValues IEnumerable<String>

A sequence of String values to match against.

Returns

Boolean

true if 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, 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

value String

The String to extend.

strings String[]

A sequence of String values to match against.

Returns

Boolean

true if 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

value String

The String to extend.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

strings IEnumerable<String>

A sequence of String values to match against.

Returns

Boolean

true if at least one value matches the beginning of this string; otherwise, false.

StartsWith(String, StringComparison, 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

value String

The String to extend.

comparison StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

strings String[]

A sequence of String values to match against.

Returns

Boolean

true if 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

value String

The String to extend.

match String

The match that will define the stopping point.

comparisonType StringComparison

One 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

source String

The string to extend.

value String

The value to suffix source.

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

source String

The 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

input String

The String to be converted into a byte[].

setup Action<EncodingOptions>

The EncodingOptions which may be configured.

Returns

Byte[]

A byte[] that is equivalent to input.

Exceptions

ArgumentNullException

input cannot be null.

InvalidEnumArgumentException

setup was 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

value String

The String to extend.

method CasingMethod

The method to use in the conversion.

Returns

String

A String that corresponds to value with the applied conversion method.

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

value String

The String to extend.

method CasingMethod

The method to use in the conversion.

culture CultureInfo

The culture rules to apply the conversion.

Returns

String

A String that corresponds to value with the applied conversion method.

Exceptions

ArgumentNullException

value cannot be null -or- culture cannot be null.

ToEnum<TEnum>(String, Boolean)

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

value String

The String to extend.

ignoreCase Boolean

true to ignore case; false to regard case.

Returns

TEnum

An enum of type TEnum whose value is represented by value.

Type Parameters

TEnum

The type of the enumeration to convert.

Exceptions

ArgumentNullException

value is null.

ArgumentException

TEnum does not represents an enumeration.

ArgumentException

value does not represents an enumeration.

See Also

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

input String

The String to extend.

setup Action<GuidStringOptions>

The GuidStringOptions which may be configured.

Returns

Guid

A Guid that is equivalent to input.

Exceptions

ArgumentNullException

input cannot be null.

ArgumentException

input cannot be empty or consist only of white-space characters.

FormatException

The specified input was not recognized to be a GUID.

See Also

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

value String

The String to extend.

setup Action<EncodingOptions>

The EncodingOptions which need to be configured.

Returns

String

A hexadecimal String representation of the characters in value.

Remarks

ToTimeSpan(String, TimeUnit)

Converts the specified value to its equivalent TimeSpan representation.

public static TimeSpan ToTimeSpan(this string value, TimeUnit timeUnit)

Parameters

value String

The String to extend.

timeUnit TimeUnit

One of the enumeration values that specifies the outcome of the conversion.

Returns

TimeSpan

A TimeSpan that corresponds to value from timeUnit.

Exceptions

ArgumentNullException

value is null.

OverflowException

The value paired with timeUnit is outside its valid range.

ArgumentOutOfRangeException

timeUnit was 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

value String

The String to extend.

uriKind UriKind

Specifies whether the URI string is a relative URI, absolute URI, or is indeterminate.

Returns

Uri

A Uri that corresponds to value and uriKind.

Exceptions

ArgumentNullException

value cannot be null.

ArgumentException

value cannot 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

value String

The String to extend.

Returns

String

The string that remains after all occurrences of white-space characters are removed from the specified value.

Exceptions

ArgumentNullException

value is null.

TrimAll(String, 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

value String

The String to extend.

trimChars Char[]

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 trimChars parameter are removed from the specified value.

Exceptions

ArgumentNullException

value is null.