Class StringExtensions
Text-related extension methods for
Inheritance
Namespace: X10D.Text
Assembly: X10D.dll
Syntax
public static class StringExtensions : object
Methods
| Improve this Doc View SourceAsNullIfEmpty(Nullable<String>)
Normalizes a string which may be either null or empty to null.
Declaration
public static string? AsNullIfEmpty(this string? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The value to normalize. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | null if |
AsNullIfWhiteSpace(Nullable<String>)
Normalizes a string which may be either null, empty, or consisting of only whitespace, to null.
Declaration
public static string? AsNullIfWhiteSpace(this string? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The value to normalize. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | null if |
Base64Decode(String)
Converts the specified string, which encodes binary data as base-64 digits, to an equivalent plain text string.
Declaration
public static string Base64Decode(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The base-64 string to convert. |
Returns
Type | Description |
---|---|
System.String | The plain text string representation of |
Base64Encode(String)
Converts the current string to its equivalent string representation that is encoded with base-64 digits.
Declaration
public static string Base64Encode(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The plain text string to convert. |
Returns
Type | Description |
---|---|
System.String | The string representation, in base 64, of |
ChangeEncoding(String, Encoding, Encoding)
Converts this string from one encoding to another.
Declaration
public static string ChangeEncoding(this string value, Encoding sourceEncoding, Encoding destinationEncoding)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The input string. |
Encoding | sourceEncoding | The input encoding. |
Encoding | destinationEncoding | The output encoding. |
Returns
Type | Description |
---|---|
System.String | Returns a new |
ConcatIf(Nullable<String>, Func<Boolean>, Func<Nullable<String>, Nullable<String>>)
Appends a string to the current string if the specified condition evaluates to true.
Declaration
public static string? ConcatIf(this string? value, Func<bool> conditionFactory, Func<string?, string?> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The current string. |
Func<System.Boolean> | conditionFactory | The function that returns the condition to evaluate. |
Func<System.Nullable<System.String>, System.Nullable<System.String>> | valueFactory | The function that returns the string to append if the condition is true, with |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | The concatenated string. |
ConcatIf(Nullable<String>, Func<Boolean>, Func<Nullable<String>>)
Appends a string to the current string if the specified condition evaluates to true.
Declaration
public static string? ConcatIf(this string? value, Func<bool> conditionFactory, Func<string?> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The current string. |
Func<System.Boolean> | conditionFactory | The function that returns the condition to evaluate. |
Func<System.Nullable<System.String>> | valueFactory | The function that returns the string to append if the condition is true. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | The concatenated string. |
ConcatIf(Nullable<String>, Func<Boolean>, Nullable<String>)
Appends a string to the current string if the specified condition evaluates to true.
Declaration
public static string? ConcatIf(this string? value, Func<bool> conditionFactory, string? appendValue)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The current string. |
Func<System.Boolean> | conditionFactory | The function that returns the condition to evaluate. |
System.Nullable<System.String> | appendValue | The string to append if the condition is true. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | The concatenated string. |
ConcatIf(Nullable<String>, Func<Nullable<String>, Boolean>, Func<Nullable<String>, Nullable<String>>)
Appends a string to the current string if the specified condition evaluates to true.
Declaration
public static string? ConcatIf(this string? value, Func<string?, bool> conditionFactory, Func<string?, string?> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The current string. |
Func<System.Nullable<System.String>, System.Boolean> | conditionFactory | The function that returns the condition to evaluate, with |
Func<System.Nullable<System.String>, System.Nullable<System.String>> | valueFactory | The function that returns the string to append if the condition is true, with |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | The concatenated string. |
ConcatIf(Nullable<String>, Func<Nullable<String>, Boolean>, Func<Nullable<String>>)
Appends a string to the current string if the specified condition evaluates to true.
Declaration
public static string? ConcatIf(this string? value, Func<string?, bool> conditionFactory, Func<string?> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The current string. |
Func<System.Nullable<System.String>, System.Boolean> | conditionFactory | The function that returns the condition to evaluate, with |
Func<System.Nullable<System.String>> | valueFactory | The function that returns the string to append if the condition is true. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | The concatenated string. |
ConcatIf(Nullable<String>, Func<Nullable<String>, Boolean>, Nullable<String>)
Appends a string to the current string if the specified condition evaluates to true.
Declaration
public static string? ConcatIf(this string? value, Func<string?, bool> conditionFactory, string? appendValue)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The current string. |
Func<System.Nullable<System.String>, System.Boolean> | conditionFactory | The function that returns the condition to evaluate, with |
System.Nullable<System.String> | appendValue | The string to append if the condition is true. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | The concatenated string. |
ConcatIf(Nullable<String>, Boolean, Func<Nullable<String>, Nullable<String>>)
Appends a string to the current string if the specified condition evaluates to true.
Declaration
public static string? ConcatIf(this string? value, bool condition, Func<string?, string?> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The current string. |
System.Boolean | condition | The condition to evaluate. |
Func<System.Nullable<System.String>, System.Nullable<System.String>> | valueFactory | The function that returns the string to append if the condition is true, with |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | The concatenated string. |
ConcatIf(Nullable<String>, Boolean, Func<Nullable<String>>)
Appends a string to the current string if the specified condition evaluates to true.
Declaration
public static string? ConcatIf(this string? value, bool condition, Func<string?> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The current string. |
System.Boolean | condition | The condition to evaluate. |
Func<System.Nullable<System.String>> | valueFactory | The function that returns the string to append if the condition is true. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | The concatenated string. |
ConcatIf(Nullable<String>, Boolean, Nullable<String>)
Appends a string to the current string if the specified condition evaluates to true.
Declaration
public static string? ConcatIf(this string? value, bool condition, string? appendValue)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The current string. |
System.Boolean | condition | The condition to evaluate. |
System.Nullable<System.String> | appendValue | The string to append if the condition is true. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> | The concatenated string. |
CountSubstring(ReadOnlySpan<Char>, Char)
Counts the occurrences of a character within the current character span.
Declaration
public static int CountSubstring(this ReadOnlySpan<char> haystack, char needle)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<System.Char> | haystack | The haystack search space. |
System.Char | needle | The character to count. |
Returns
Type | Description |
---|---|
System.Int32 | An integer representing the count of |
CountSubstring(Span<Char>, Char)
Counts the occurrences of a character within the current character span.
Declaration
public static int CountSubstring(this Span<char> haystack, char needle)
Parameters
Type | Name | Description |
---|---|---|
Span<System.Char> | haystack | The haystack search space. |
System.Char | needle | The character to count. |
Returns
Type | Description |
---|---|
System.Int32 | An integer representing the count of |
CountSubstring(String, Char)
Counts the occurrences of a character within the current string.
Declaration
public static int CountSubstring(this string haystack, char needle)
Parameters
Type | Name | Description |
---|---|---|
System.String | haystack | The haystack search space. |
System.Char | needle | The character to count. |
Returns
Type | Description |
---|---|
System.Int32 | An integer representing the count of |
CountSubstring(String, Nullable<String>)
Counts the occurrences of a substring within the current string.
Declaration
public static int CountSubstring(this string haystack, string? needle)
Parameters
Type | Name | Description |
---|---|---|
System.String | haystack | The haystack search space. |
System.Nullable<System.String> | needle | The substring to count. |
Returns
Type | Description |
---|---|
System.Int32 | An integer representing the count of |
CountSubstring(String, Nullable<String>, StringComparison)
Counts the occurrences of a substring within the current string, using a specified string comparison method.
Declaration
public static int CountSubstring(this string haystack, string? needle, StringComparison comparison)
Parameters
Type | Name | Description |
---|---|---|
System.String | haystack | The haystack search space. |
System.Nullable<System.String> | needle | The substring to count. |
StringComparison | comparison | The string comparison method used for determining substring count. |
Returns
Type | Description |
---|---|
System.Int32 | An integer representing the count of |
EnsureEndsWith(Nullable<String>, Char)
Ensures that the current string starts with a specified substring.
Declaration
public static string EnsureEndsWith(this string? value, char substring)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The string to check. |
System.Char | substring | The substring to prepend, if the current string does not already start with it. |
Returns
Type | Description |
---|---|
System.String | The combined string. |
EnsureEndsWith(Nullable<String>, Char, StringComparison)
Ensures that the current string starts with a specified substring.
Declaration
public static string EnsureEndsWith(this string? value, char substring, StringComparison comparisonType)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The string to check. |
System.Char | substring | The substring to prepend, if the current string does not already start with it. |
StringComparison | comparisonType | One of the enumeration values that determines how the substring is compared. |
Returns
Type | Description |
---|---|
System.String | The combined string. |
EnsureEndsWith(Nullable<String>, String)
Ensures that the current string starts with a specified substring.
Declaration
public static string EnsureEndsWith(this string? value, string substring)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The string to check. |
System.String | substring | The substring to prepend, if the current string does not already start with it. |
Returns
Type | Description |
---|---|
System.String | The combined string. |
EnsureEndsWith(Nullable<String>, String, StringComparison)
Ensures that the current string starts with a specified substring.
Declaration
public static string EnsureEndsWith(this string? value, string substring, StringComparison comparisonType)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The string to check. |
System.String | substring | The substring to prepend, if the current string does not already start with it. |
StringComparison | comparisonType | One of the enumeration values that determines how the substring is compared. |
Returns
Type | Description |
---|---|
System.String | The combined string. |
EnsureStartsWith(Nullable<String>, Char)
Ensures that the current string starts with a specified substring.
Declaration
public static string EnsureStartsWith(this string? value, char substring)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The string to check. |
System.Char | substring | The substring to prepend, if the current string does not already start with it. |
Returns
Type | Description |
---|---|
System.String | The combined string. |
EnsureStartsWith(Nullable<String>, Char, StringComparison)
Ensures that the current string starts with a specified substring.
Declaration
public static string EnsureStartsWith(this string? value, char substring, StringComparison comparisonType)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The string to check. |
System.Char | substring | The substring to prepend, if the current string does not already start with it. |
StringComparison | comparisonType | One of the enumeration values that determines how the substring is compared. |
Returns
Type | Description |
---|---|
System.String | The combined string. |
EnsureStartsWith(Nullable<String>, String)
Ensures that the current string starts with a specified substring.
Declaration
public static string EnsureStartsWith(this string? value, string substring)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The string to check. |
System.String | substring | The substring to prepend, if the current string does not already start with it. |
Returns
Type | Description |
---|---|
System.String | The combined string. |
EnsureStartsWith(Nullable<String>, String, StringComparison)
Ensures that the current string starts with a specified substring.
Declaration
public static string EnsureStartsWith(this string? value, string substring, StringComparison comparisonType)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The string to check. |
System.String | substring | The substring to prepend, if the current string does not already start with it. |
StringComparison | comparisonType | One of the enumeration values that determines how the substring is compared. |
Returns
Type | Description |
---|---|
System.String | The combined string. |
EnumParse<T>(String)
Parses a
Declaration
public static T EnumParse<T>(this string value)
where T : struct, Enum
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The |
Returns
Type | Description |
---|---|
T | The |
Type Parameters
Name | Description |
---|---|
T | The type of the |
Remarks
Credit for this method goes to Scott Dorman: (http://geekswithblogs.net/sdorman/Default.aspx).
EnumParse<T>(String, Boolean)
Parses a
Declaration
public static T EnumParse<T>(this string value, bool ignoreCase)
where T : struct, Enum
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The |
System.Boolean | ignoreCase | Whether or not to ignore casing. |
Returns
Type | Description |
---|---|
T | The |
Type Parameters
Name | Description |
---|---|
T | The type of the |
Remarks
Credit for this method goes to Scott Dorman: (http://geekswithblogs.net/sdorman/Default.aspx).
FromJson<T>(String, Nullable<JsonSerializerOptions>)
Returns an object from the specified JSON string.
Declaration
public static T FromJson<T>(this string value, JsonSerializerOptions? options = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The JSON to convert. |
System.Nullable<JsonSerializerOptions> | options | The JSON serialization options. |
Returns
Type | Description |
---|---|
T | An object constructed from the JSON string, or null if deserialization could not be performed. |
Type Parameters
Name | Description |
---|---|
T | The type of the value to deserialize. |
GetBytes(String)
Gets a
Declaration
public static byte[] GetBytes(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to convert. |
Returns
Type | Description |
---|---|
System.Byte[] | Returns a |
GetBytes(String, Encoding)
Gets a
Declaration
public static byte[] GetBytes(this string value, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to convert. |
Encoding | encoding | The encoding to use. |
Returns
Type | Description |
---|---|
System.Byte[] | Returns a |
IsEmoji(String)
Returns a value indicating whether this string constitutes an emoji.
Declaration
public static bool IsEmoji(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The input string. |
Returns
Type | Description |
---|---|
System.Boolean | true if this string is an emoji; otherwise, false. |
IsEmpty(String)
Returns a value indicating whether the current string represents an empty string.
Declaration
public static bool IsEmpty(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The value to check. |
Returns
Type | Description |
---|---|
System.Boolean | true if |
IsLower(String)
Determines if all alpha characters in this string are considered lowercase.
Declaration
public static bool IsLower(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The input string. |
Returns
Type | Description |
---|---|
System.Boolean | true if all alpha characters in this string are lowercase; otherwise, false. |
IsNullOrEmpty(Nullable<String>)
Returns a value indicating whether the current string is null (langword_csharp_Nothing in Visual Basic), or represents an empty string.
Declaration
public static bool IsNullOrEmpty(this string? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The value to check. |
Returns
Type | Description |
---|---|
System.Boolean | true if |
IsNullOrWhiteSpace(Nullable<String>)
Returns a value indicating whether the current string is null (langword_csharp_Nothing in Visual Basic), represents an empty string, or consists of only whitespace characters.
Declaration
public static bool IsNullOrWhiteSpace(this string? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The value to check. |
Returns
Type | Description |
---|---|
System.Boolean | true if |
IsPalindrome(String)
Determines whether the current string is considered palindromic; that is, the letters within the string are the same when reversed.
Declaration
public static bool IsPalindrome(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The value to check. |
Returns
Type | Description |
---|---|
System.Boolean | true if |
IsUpper(String)
Determines if all alpha characters in this string are considered uppercase.
Declaration
public static bool IsUpper(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The input string. |
Returns
Type | Description |
---|---|
System.Boolean | true if all alpha characters in this string are uppercase; otherwise, false. |
IsWhiteSpace(String)
Returns a value indicating whether the current string represents an empty string, or consists of only whitespace characters.
Declaration
public static bool IsWhiteSpace(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The value to check. |
Returns
Type | Description |
---|---|
System.Boolean | true if |
Randomize(String, Int32, Nullable<Random>)
Returns a new string of a specified length by randomly selecting characters from the current string.
Declaration
public static string Randomize(this string source, int length, Random? random = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | source | The pool of characters to use. |
System.Int32 | length | The length of the new string returned. |
System.Nullable<Random> | random | The |
Returns
Type | Description |
---|---|
System.String | A new string whose length is equal to |
Repeat(String, Int32)
Repeats a string a specified number of times.
Declaration
public static string Repeat(this string value, int count)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to repeat. |
System.Int32 | count | The repeat count. |
Returns
Type | Description |
---|---|
System.String | A string containing |
Repeat(String, Int32, Span<Char>)
Repeats a string a specified number of times, writing the result to a span of characters.
Declaration
public static void Repeat(this string value, int count, Span<char> destination)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to repeat. |
System.Int32 | count | The repeat count. |
Span<System.Char> | destination | The destination span to write to. |
Reverse(String)
Reverses the current string.
Declaration
public static string Reverse(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to reverse. |
Returns
Type | Description |
---|---|
System.String | A |
Shuffled(String, Nullable<Random>)
Shuffles the characters in the string.
Declaration
public static string Shuffled(this string value, Random? random = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to shuffle. |
System.Nullable<Random> | random | The |
Returns
Type | Description |
---|---|
System.String | A new |
Split(String, Int32)
Splits the chunkSize
in length.
Declaration
public static IEnumerable<string> Split(this string value, int chunkSize)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to split. |
System.Int32 | chunkSize | The maximum length of each string in the returned result. |
Returns
Type | Description |
---|---|
IEnumerable<System.String> | Returns an |
StartsWithAny(Nullable<String>, StringComparison, String[])
Determines whether the beginning of this string instance matches any of the specified strings when compared using the specified comparison option.
Declaration
public static bool StartsWithAny(this string? value, StringComparison comparison, params string[] startValues)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The value to compare. |
StringComparison | comparison | One of the enumeration values that determines how this string and value are compared. |
System.String[] | startValues | An array of string to compare. |
Returns
Type | Description |
---|---|
System.Boolean | true if |
StartsWithAny(Nullable<String>, String[])
Determines whether the beginning of this string instance matches any of the specified strings using the current culture for comparison.
Declaration
public static bool StartsWithAny(this string? value, params string[] startValues)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The value to compare. |
System.String[] | startValues | An array of string to compare. |
Returns
Type | Description |
---|---|
System.Boolean | true if |
WithEmptyAlternative(Nullable<String>, Nullable<String>)
Normalizes a string which may be either null or empty to a specified alternative.
Declaration
public static string? WithEmptyAlternative(this string? value, string? alternative)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The value to normalize. |
System.Nullable<System.String> | alternative | The alternative string. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> |
|
WithWhiteSpaceAlternative(Nullable<String>, Nullable<String>)
Normalizes a string which may be either null, empty, or consisting of only whitespace, to a specified alternative.
Declaration
public static string? WithWhiteSpaceAlternative(this string? value, string? alternative)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.String> | value | The value to normalize. |
System.Nullable<System.String> | alternative | The alternative string. |
Returns
Type | Description |
---|---|
System.Nullable<System.String> |
|