Class StringExtensions
Extensions for the string class.
Inherited Members
Namespace: TheXDS.MCART.Types.Extensions
Assembly: MCART.dll
Syntax
public static class StringExtensions
Methods
| Edit this page View SourceCapitalize(string, bool)
Gets a capitalized string from the provided string.
Declaration
public static string Capitalize(this string str, bool keepCasing = false)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to capitalize. |
bool | keepCasing | If set to true, maintains the casing of the rest of the characters in the string. |
Returns
Type | Description |
---|---|
string | A new capitalized string from the provided string. |
Chop(string, string)
Removes an occurrence of a string from the ends of another.
Declaration
public static string Chop(this string str, string toChop)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
string | toChop | Value to remove. |
Returns
Type | Description |
---|---|
string | A string that does not start or end with
|
ChopAny(string, params string[])
Removes an occurrence of any specified strings from the ends of another.
Declaration
public static string ChopAny(this string str, params string[] toChop)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
string[] | toChop | Values to remove. |
Returns
Type | Description |
---|---|
string | A string that does not start or end with
|
ChopEnd(string, string)
Removes an occurrence of a string from the end of another.
Declaration
public static string ChopEnd(this string str, string toChop)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
string | toChop | Value to remove. |
Returns
Type | Description |
---|---|
string | A string that does not end with
|
ChopEndAny(string, params string[])
Removes an occurrence of a string from the end of another.
Declaration
public static string ChopEndAny(this string str, params string[] toChop)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
string[] | toChop | Values to remove. |
Returns
Type | Description |
---|---|
string | A string that does not end with |
ChopStart(string, string)
Removes an occurrence of a string from the beginning of another.
Declaration
public static string ChopStart(this string str, string toChop)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
string | toChop | Value to remove. |
Returns
Type | Description |
---|---|
string | A string that does not start with |
ChopStartAny(string, params string[])
Removes an occurrence of any specified strings from the beginning of another.
Declaration
public static string ChopStartAny(this string str, params string[] toChop)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
string[] | toChop | Values to remove. |
Returns
Type | Description |
---|---|
string | A string that does not start with |
ContainsAny(string, params char[])
Determines if a string contains any of the specified characters.
Declaration
public static bool ContainsAny(this string stringToCheck, params char[] chars)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
char[] | chars | Characters to search for. |
Returns
Type | Description |
---|---|
bool | true if the string contains any of the characters, false otherwise. |
ContainsAny(string, IEnumerable<char>, out int)
Determines if a string contains any of the specified characters.
Declaration
public static bool ContainsAny(this string stringToCheck, IEnumerable<char> chars, out int argNum)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
IEnumerable<char> | chars | Characters to search for. |
int | argNum | Output parameter. If |
Returns
Type | Description |
---|---|
bool | true if the string contains any of the characters, false otherwise. |
ContainsAny(string, IEnumerable<string>)
Determines if a string contains any of the specified strings.
Declaration
public static bool ContainsAny(this string stringToCheck, IEnumerable<string> strings)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
IEnumerable<string> | strings | Strings to search for. |
Returns
Type | Description |
---|---|
bool | true if the string contains any of the characters, false otherwise. |
ContainsAny(string, IEnumerable<string>, out int)
Determines if a string contains any of the specified strings.
Declaration
public static bool ContainsAny(this string stringToCheck, IEnumerable<string> strings, out int argNum)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
IEnumerable<string> | strings | Strings to search for. |
int | argNum | Output parameter. If |
Returns
Type | Description |
---|---|
bool | true if the string contains any of the characters, false otherwise. |
ContainsAny(string, out int, params char[])
Determines if a string contains any of the specified characters.
Declaration
public static bool ContainsAny(this string stringToCheck, out int argNum, params char[] chars)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
int | argNum | Output parameter. If |
char[] | chars | Characters to search for. |
Returns
Type | Description |
---|---|
bool | true if the string contains any of the characters, false otherwise. |
ContainsAny(string, out int, params string[])
Determines if a string contains any of the specified strings.
Declaration
public static bool ContainsAny(this string stringToCheck, out int argNum, params string[] strings)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
int | argNum | Output parameter. If |
string[] | strings | Strings to search for. |
Returns
Type | Description |
---|---|
bool | true if the string contains any of the characters, false otherwise. |
ContainsAny(string, params string[])
Determines if a string contains any of the specified strings.
Declaration
public static bool ContainsAny(this string stringToCheck, params string[] strings)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
string[] | strings | Strings to search for. |
Returns
Type | Description |
---|---|
bool | true if the string contains any of the characters, false otherwise. |
ContainsLetters(string)
Checks if a string contains letters.
Declaration
public static bool ContainsLetters(this string stringToCheck)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
Returns
Type | Description |
---|---|
bool |
ContainsLetters(string, bool)
Checks if a string contains letters.
Declaration
public static bool ContainsLetters(this string stringToCheck, bool ucase)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
bool | ucase | Optional. Specifies the type of check to perform. If set to true, only uppercase characters are considered; if set to false, only lowercase characters are considered. If omitted or set to null, both cases are considered. |
Returns
Type | Description |
---|---|
bool |
ContainsNumbers(string)
Checks if a string contains numbers.
Declaration
public static bool ContainsNumbers(this string stringToCheck)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
Returns
Type | Description |
---|---|
bool |
CouldItBe(string, string)
Checks if a name could be another specified name.
Declaration
public static float CouldItBe(this string checkName, string actualName)
Parameters
Type | Name | Description |
---|---|---|
string | checkName | Name to check. |
string | actualName | Known actual name. |
Returns
Type | Description |
---|---|
float | A percentage value representing the probability that
|
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Occurs when |
CouldItBe(string, string, float)
Checks if a name could be another specified name.
Declaration
public static float CouldItBe(this string checkName, string actualName, float tolerance)
Parameters
Type | Name | Description |
---|---|---|
string | checkName | Name to check. |
string | actualName | Known actual name. |
float | tolerance | Optional. A float between 0.0 and 1.0 that sets the minimum acceptable level of similarity. If not specified, it defaults to 75% (0.75). |
Returns
Type | Description |
---|---|
float | A value representing the probability that
|
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Occurs when |
ArgumentNullException | Occurs when |
CountChars(string, params char[])
Counts the characters that a string contains.
Declaration
public static int CountChars(this string stringToCheck, params char[] chars)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
char[] | chars | Characters to count. |
Returns
Type | Description |
---|---|
int | An int with the total number of
|
CountChars(string, string)
Counts the characters that a string contains.
Declaration
public static int CountChars(this string stringToCheck, string chars)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
string | chars | Characters to count. |
Returns
Type | Description |
---|---|
int | An int with the total number of
characters of |
IsBinary(string)
Determines if a string contains a binary value.
Declaration
public static bool IsBinary(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
Returns
Type | Description |
---|---|
bool | true if the string contains a value that can be interpreted as a binary number, false otherwise. |
IsEmpty(string?)
Determines if a string is empty.
Declaration
public static bool IsEmpty(this string? stringToCheck)
Parameters
Type | Name | Description |
---|---|---|
string | stringToCheck | String to check. |
Returns
Type | Description |
---|---|
bool |
IsFormattedAs(string, string)
Checks if the string has a basic alphanumeric format equal to the specified one.
Declaration
public static bool IsFormattedAs(this string checkString, string format)
Parameters
Type | Name | Description |
---|---|---|
string | checkString | string to check. |
string | format | Basic alphanumeric format to compare against. |
Returns
Type | Description |
---|---|
bool | true if the string's format is equal to the specified one, false otherwise. |
IsFormattedAs(string, string, bool)
Checks if the string has a basic alphanumeric format equal to the specified one.
Declaration
public static bool IsFormattedAs(this string checkString, string format, bool checkCase)
Parameters
Type | Name | Description |
---|---|---|
string | checkString | string to check. |
string | format | Basic alphanumeric format to compare against. |
bool | checkCase | If set to true, a case-sensitive evaluation of the string will be performed. |
Returns
Type | Description |
---|---|
bool | true if the string's format is equal to the specified one, false otherwise. |
IsHex(string)
Determines if a string contains a hexadecimal value.
Declaration
public static bool IsHex(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
Returns
Type | Description |
---|---|
bool | true if the string contains a value that can be interpreted as a hexadecimal number, false otherwise. |
Left(string, int)
Gets a string that contains the specified number of characters from the left side of the string.
Declaration
public static string Left(this string @string, int length)
Parameters
Type | Name | Description |
---|---|---|
string | string | Instance of string to process. |
int | length | Length of characters to obtain. |
Returns
Type | Description |
---|---|
string | A string that contains the specified number of characters from the left side of the string. |
Likeness(string, string)
Calculates the percentage of similarity between two strings.
Declaration
public static float Likeness(this string ofString, string toString)
Parameters
Type | Name | Description |
---|---|---|
string | ofString | String A to compare. |
string | toString | String B to compare. |
Returns
Type | Description |
---|---|
float | The percentage of similarity between the two strings. |
Likeness(string, string, int)
Calculates the percentage of similarity between two strings.
Declaration
public static float Likeness(this string ofString, string toString, int tolerance)
Parameters
Type | Name | Description |
---|---|---|
string | ofString | String A to compare. |
string | toString | String B to compare. |
int | tolerance | Tolerance range for comparison. Represents the maximum allowed distance for each character that still makes the strings similar. |
Returns
Type | Description |
---|---|
float | The percentage of similarity between the two strings. |
OrElse(string?, string?)
Returns the first non-empty string from the provided strings.
Declaration
public static string? OrElse(string? source, string? emptyRetVal)
Parameters
Type | Name | Description |
---|---|---|
string | source | Source string. |
string | emptyRetVal | Value to return if all source strings are empty. |
Returns
Type | Description |
---|---|
string | Non-empty string or default value. |
OrEmpty(string?)
Ensures to return Empty if the string is empty or null.
Declaration
public static string OrEmpty(this string? str)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to return. |
Returns
Type | Description |
---|---|
string | The string, or Empty if the string is empty or null. |
OrEmpty(string?, string)
Ensures to return Empty if the string is empty or null.
Declaration
public static string OrEmpty(this string? str, string notEmptyFormat)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to return. |
string | notEmptyFormat | Format to apply in case the string is not Empty. |
Returns
Type | Description |
---|---|
string | The string, or Empty if the string is empty or null. |
OrNull(string?)
Ensures to return null if the string is empty.
Declaration
public static string? OrNull(this string? str)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to return. |
Returns
Type | Description |
---|---|
string | The string, or null if the string is empty. |
OrNull(string?, string)
Ensures to return null if the string is empty.
Declaration
public static string? OrNull(this string? str, string notNullFormat)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to return. |
string | notNullFormat | Format to apply in case the string is not null. |
Returns
Type | Description |
---|---|
string | The string, or null if the string is empty. |
Right(string, int)
Gets a string that contains the specified number of characters from the left side of the string.
Declaration
public static string Right(this string @string, int length)
Parameters
Type | Name | Description |
---|---|---|
string | string | Instance of string to process. |
int | length | Number of characters to get. |
Returns
Type | Description |
---|---|
string | A string that contains the specified number of characters from the left side of the string. |
Separate(string, char)
Separates each character in a string with the specified char.
Declaration
public static string Separate(this string str, char separationChar)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to process. |
char | separationChar | Character to use for separation. |
Returns
Type | Description |
---|---|
string | A string whose characters have been separated by the specified char. |
Spell(string)
Separates each character in a string with a space.
Declaration
public static string Spell(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to process. |
Returns
Type | Description |
---|---|
string | A string whose characters have been separated by a space. |
Split(string, int)
Splits this string into chunks of the specified size.
Declaration
public static IEnumerable<string> Split(this string str, int chunkSize)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to split. |
int | chunkSize | Chunk size. |
Returns
Type | Description |
---|---|
IEnumerable<string> | Enumerable collection of string chunks. |
SplitByCase(string?)
Splits a string based on the casing of its characters.
Declaration
public static IEnumerable<string> SplitByCase(this string? name)
Parameters
Type | Name | Description |
---|---|---|
string | name | String to split. |
Returns
Type | Description |
---|---|
IEnumerable<string> | An enumeration of strings resulting from splitting by case. |
StartsWithAny(string, IEnumerable<string>)
Determines whether the string starts with any of the specified strings.
Declaration
public static bool StartsWithAny(this string str, IEnumerable<string> strings)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
IEnumerable<string> | strings | Collection of initial strings to determine. |
Returns
Type | Description |
---|---|
bool | true if the string starts with any of the specified strings, otherwise false. |
StartsWithAny(string, IEnumerable<string>, bool)
Determines whether the string starts with any of the specified strings, with case sensitivity control.
Declaration
public static bool StartsWithAny(this string str, IEnumerable<string> strings, bool ignoreCase)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
IEnumerable<string> | strings | Collection of initial strings to determine. |
bool | ignoreCase | If set to true, the comparison is case-insensitive; if set to false, the comparison is case-sensitive. |
Returns
Type | Description |
---|---|
bool | true if the string starts with any of the specified strings, otherwise false. |
StartsWithAny(string, IEnumerable<string>, bool, CultureInfo)
Determines whether the string starts with any of the specified strings, with case sensitivity and culture control.
Declaration
public static bool StartsWithAny(this string str, IEnumerable<string> strings, bool ignoreCase, CultureInfo culture)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
IEnumerable<string> | strings | Collection of initial strings to determine. |
bool | ignoreCase | If set to true, the comparison is case-insensitive; if set to false, the comparison is case-sensitive. |
CultureInfo | culture | The culture to use for the comparison. |
Returns
Type | Description |
---|---|
bool | true if the string starts with any of the specified strings, otherwise false. |
StartsWithAny(string, IEnumerable<string>, StringComparison)
Determines whether the string starts with any of the specified strings, using specified comparison options.
Declaration
public static bool StartsWithAny(this string str, IEnumerable<string> strings, StringComparison comparison)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
IEnumerable<string> | strings | Collection of initial strings to determine. |
StringComparison | comparison | Specifies how the string comparison should be performed (case, culture). |
Returns
Type | Description |
---|---|
bool | true if the string starts with any of the specified strings, otherwise false. |
StartsWithAny(string, params string[])
Determines whether the string starts with any of the specified strings.
Declaration
public static bool StartsWithAny(this string str, params string[] strings)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to check. |
string[] | strings | Collection of initial strings to determine. |
Returns
Type | Description |
---|---|
bool | true if the string starts with any of the specified strings, otherwise false. |
TextWrap(string)
Wraps long text content into lines of up to 80 characters.
Declaration
public static string[] TextWrap(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to wrap. |
Returns
Type | Description |
---|---|
string[] | An array of strings with the original string's content wrapped into rows of up to 80 characters. |
TextWrap(string, int)
Wraps long text content into lines.
Declaration
public static string[] TextWrap(this string str, int width)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to wrap. |
int | width | Number of characters allowed per row. By default, it's 80 characters per column. |
Returns
Type | Description |
---|---|
string[] | An array of strings with the original string's content wrapped into rows. |
ToBase64(string)
Converts this string into its base64 representation using UTF8.
Declaration
public static string ToBase64(this string @string)
Parameters
Type | Name | Description |
---|---|---|
string | string | String to convert. |
Returns
Type | Description |
---|---|
string | A base64 formatted string containing the content of the original string. |
ToBase64(string, Encoding)
Converts this string into its base64 representation, specifying the Encoding to use for the conversion.
Declaration
public static string ToBase64(this string @string, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
string | string | String to convert. |
Encoding | encoding | String encoding. |
Returns
Type | Description |
---|---|
string | A base64 formatted string containing the content of the original string. |
ToSecureString(string)
Converts a string to a SecureString.
Declaration
public static SecureString ToSecureString(this string @string)
Parameters
Type | Name | Description |
---|---|---|
string | string | string to convert. |
Returns
Type | Description |
---|---|
SecureString | A SecureString containing all the original characters of the provided string. |
ToStream(string)
Declaration
public static Stream ToStream(this string @string)
Parameters
Type | Name | Description |
---|---|---|
string | string | String to convert. |
Returns
Type | Description |
---|---|
Stream | A Stream containing the content of the string. |
Remarks
Although strings in .NET Framework are UTF16, certain common functions prefer working with UTF8 encoded strings, so this method uses that encoding for conversion.
ToStream(string, Encoding)
Declaration
public static Stream ToStream(this string @string, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
string | string | String to convert. |
Encoding | encoding | String encoding. |
Returns
Type | Description |
---|---|
Stream | A Stream containing the content of the string. |
TokenSearch(string, string)
Performs a tokenized search on the specified string.
Declaration
public static bool TokenSearch(this string str, string searchTerms)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string in which to search. |
string | searchTerms | Search terms. |
Returns
Type | Description |
---|---|
bool | true if the string matches any of the search terms, otherwise false. |
TokenSearch(string, string, char, SearchOptions)
Performs a tokenized search on the specified string.
Declaration
public static bool TokenSearch(this string str, string searchTerms, char separator, StringExtensions.SearchOptions options)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string in which to search. |
string | searchTerms | Search terms. |
char | separator | Token separator. |
StringExtensions.SearchOptions | options | Search options. |
Returns
Type | Description |
---|---|
bool | true if the string matches any of the specified search terms and options, otherwise false. |
TokenSearch(string, string, SearchOptions)
Performs a tokenized search on the specified string.
Declaration
public static bool TokenSearch(this string str, string searchTerms, StringExtensions.SearchOptions options)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string in which to search. |
string | searchTerms | Search terms. |
StringExtensions.SearchOptions | options | Search options. |
Returns
Type | Description |
---|---|
bool | true if the string matches any of the specified search terms and options, otherwise false. |
Truncate(string, int)
Truncates the length of a string to a maximum of
length
characters.
Declaration
public static string Truncate(this string str, int length)
Parameters
Type | Name | Description |
---|---|---|
string | str | String to truncate. |
int | length | Maximum length of the string. |
Returns
Type | Description |
---|---|
string |
WildCardToRegular(string)
Converts a wildcard pattern to a regular expression pattern.
Declaration
public static string WildCardToRegular(string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | Wildcard pattern. |
Returns
Type | Description |
---|---|
string | Regular expression pattern. |
Without(string, params char[])
Returns a new string without the specified characters.
Declaration
public static string Without(this string @string, params char[] chars)
Parameters
Type | Name | Description |
---|---|---|
string | string | String to process. |
char[] | chars | Characters to remove. |
Returns
Type | Description |
---|---|
string | A string that does not contain any of the specified characters. |
Without(string, params string[])
Returns a new string without the specified substrings.
Declaration
public static string Without(this string @string, params string[] strings)
Parameters
Type | Name | Description |
---|---|---|
string | string | String to process. |
string[] | strings | Substrings to remove. |
Returns
Type | Description |
---|---|
string | A string that does not contain any of the specified substrings. |