Show / Hide Table of Contents

Class StringExtensions

Extensions for the string class.

Inheritance
object
StringExtensions
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: TheXDS.MCART.Types.Extensions
Assembly: MCART.dll
Syntax
public static class StringExtensions

Methods

| Edit this page View Source

Capitalize(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.

| Edit this page View Source

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 toChop.

| Edit this page View Source

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 toChop.

| Edit this page View Source

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 toChop.

| Edit this page View Source

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 toChop.

| Edit this page View Source

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 toChop.

| Edit this page View Source

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 toChop.

| Edit this page View Source

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.

| Edit this page View Source

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 stringToCheck contains any character specified in chars, the index of the contained argument will be returned; otherwise, -1 is returned.

Returns
Type Description
bool

true if the string contains any of the characters, false otherwise.

| Edit this page View Source

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.

| Edit this page View Source

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 stringToCheck contains any character specified in strings, the index of the contained argument will be returned; otherwise, -1 is returned.

Returns
Type Description
bool

true if the string contains any of the characters, false otherwise.

| Edit this page View Source

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 stringToCheck contains any character specified in chars, the index of the contained argument will be returned; otherwise, -1 is returned.

char[] chars

Characters to search for.

Returns
Type Description
bool

true if the string contains any of the characters, false otherwise.

| Edit this page View Source

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 stringToCheck contains any character specified in strings, the index of the contained argument will be returned; otherwise, -1 is returned.

string[] strings

Strings to search for.

Returns
Type Description
bool

true if the string contains any of the characters, false otherwise.

| Edit this page View Source

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.

| Edit this page View Source

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

true if the string contains letters; otherwise, false.

| Edit this page View Source

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

true if the string contains letters; otherwise, false.

| Edit this page View Source

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

true if the string contains numbers; otherwise, false.

| Edit this page View Source

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 checkName refers to the name actualName.

Exceptions
Type Condition
ArgumentNullException

Occurs when checkName or actualName are empty strings or null.

| Edit this page View Source

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 checkName refers to the name actualName.

Exceptions
Type Condition
ArgumentOutOfRangeException

Occurs when tolerance is not a value between 0.0f and 1.0f.

ArgumentNullException

Occurs when checkName or actualName are empty strings or null.

| Edit this page View Source

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 chars that appear in stringToCheck.

| Edit this page View Source

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 chars that appear in stringToCheck.

| Edit this page View Source

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.

| Edit this page View Source

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

true if the string is empty or null; otherwise, false.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

ToStream(string)

Allows obtaining the content of a string as a Stream using UTF8 encoding.

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.

| Edit this page View Source

ToStream(string, Encoding)

Allows obtaining the content of a string as a Stream using the specified 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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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
| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX