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
StringA 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
StringA String to chunk into a sequence of smaller string-slices for partitioned storage or similar.
length
Int32The 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 tovalue
.
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
Returns
- Boolean
true
if all of thevalues
occurs 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.
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
StringThe String to extend.
comparison
StringComparisonOne 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 thevalues
occurs within thevalue
; 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
StringThe String to extend.
find
CharThe Char to search within
value
.comparison
StringComparisonOne of the enumeration values that specifies the rules to use in the comparison. Default is OrdinalIgnoreCase.
Returns
- Boolean
true
if thefind
parameter occurs within thevalue
; 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
Returns
- Boolean
true
if thevalues
parameter 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
value
StringThe String to extend.
find
StringThe String to find within
value
.comparison
StringComparisonOne of the enumeration values that specifies the rules to use in the comparison. Default is OrdinalIgnoreCase.
Returns
- Boolean
true
if thefind
parameter occurs within thevalue
; 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
Returns
- Boolean
true
if any of thevalues
occurs 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, 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
StringThe String to extend.
comparison
StringComparisonOne 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 thevalues
parameter occurs within thevalue
; 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
StringThe String to extend.
comparison
StringComparisonOne 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 thevalues
occurs within thevalue
; 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
Returns
- Int32
The number of times the
character
was found in thevalue
.
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
StringThe value where characters that are not also in
second
will be returned.second
StringThe value to compare with
first
.
Returns
- String
A String that contains the set difference between
second
andfirst
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
Returns
- Boolean
true
if one thevalues
is 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
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
StringThe String to extend.
comparison
StringComparisonOne 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 thevalues
is the same as thevalue
; otherwisefalse
.
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
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
StringThe String to extend.
setup
Action<EncodingOptions>The EncodingOptions which need to be configured.
Returns
Remarks
EncodingOptions will be initialized with DefaultPreambleSequence and DefaultEncoding.
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
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
Returns
- Boolean
true
if the specifiedvalue
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
Returns
- Boolean
true
if the specifiedvalue
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
StringThe String to extend.
format
GuidFormatsA 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
- Boolean
true
if the specifiedvalue
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
Returns
- Boolean
true
if thevalue
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
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
StringThe String to extend.
style
NumberStylesA bitwise combination of NumberStyles values that indicates the permitted format of
value
.provider
IFormatProviderAn 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
CultureInfoThe culture-specific formatting information to apply on the elements within
source
.context
ITypeDescriptorContextThe 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 specifiedT
.
Returns
- Boolean
true
if elements of thesource
parameter was successfully converted; otherwisefalse
.
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
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
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
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
StringThe 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
StringThe 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 orfilter
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
StringThe String to extend.
comparison
StringComparisonOne 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 orfilter
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 orfilter
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
StringComparisonOne 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 orfilter
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
StringThe String to extend.
oldValue
StringThe String value to be replaced.
newValue
StringThe String value to replace all occurrences of
oldValue
.comparison
StringComparisonOne 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
value
StringThe 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
StringThe 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
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
StringThe String to extend.
comparison
StringComparisonOne 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
StringThe String to extend.
comparison
StringComparisonOne 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
StringThe String to extend.
match
StringThe match that will define the stopping point.
comparisonType
StringComparisonOne 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
source
StringThe 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
StringThe 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
StringThe String to extend.
method
CasingMethodThe 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
value
StringThe String to extend.
method
CasingMethodThe method to use in the conversion.
culture
CultureInfoThe culture rules to apply the conversion.
Returns
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
Returns
- TEnum
An enum of type
TEnum
whose value is represented byvalue
.
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
StringThe String to extend.
setup
Action<GuidStringOptions>The GuidStringOptions which may be configured.
Returns
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
StringThe String to extend.
setup
Action<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
value
StringThe String to extend.
timeUnit
TimeUnitOne of the enumeration values that specifies the outcome of the conversion.
Returns
Exceptions
- ArgumentNullException
value
is null.- OverflowException
The
value
paired withtimeUnit
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
StringThe String to extend.
uriKind
UriKindSpecifies whether the URI string is a relative URI, absolute URI, or is indeterminate.
Returns
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
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
StringThe 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 specifiedvalue
.
Exceptions
- ArgumentNullException
value
is null.