Class StringExtensions
Extensions for string class.
public static class StringExtensions
- Inheritance
-
objectStringExtensions
Methods
Contains(string, char)
Indicate if the string contains a character.
public static bool Contains(this string text, char value)
Parameters
Returns
- bool
A boolean indicating if at least one instance of
valueis present intext
EndsWith(string, char)
Determines whether the end of this string ends by the specified character.
public static bool EndsWith(this string stringToTest, char endChar)
Parameters
Returns
- bool
trueif the end of this string ends by the specified character,falseotherwise.
EndsWith(string, params char[])
Determines whether the end of this string ends by the specified characters.
public static bool EndsWith(this string stringToTest, params char[] endChars)
Parameters
Returns
- bool
trueif the end of this string ends by the specified character,falseotherwise.
IndexOf(StringBuilder, char)
Calculates the index of a char inside the following StringBuilder, equivalent of IndexOf(char) for a StringBuilder.
public static int IndexOf(this StringBuilder builder, char testChar)
Parameters
builderStringBuilderThe builder.
testCharcharThe test character.
Returns
- int
A position to the character found, or -1 if not found.
Exceptions
- ArgumentNullException
builder
IndexOfReverse(string, char, int)
Reports the index number, or character position, of the first occurrence of the specified Unicode character in the current String object. The search starts at a specified character position starting from the end and examines a specified number of character positions.
public static int IndexOfReverse(this string text, char charToFind, int matchCount = 1)
Parameters
textstringThe text.
charToFindcharThe character automatic find.
matchCountintThe number of match before stopping. Default is 1
Returns
- int
The character position of the value parameter for the specified character if it is found, or -1 if it is not found.
Exceptions
- ArgumentNullException
text
- ArgumentOutOfRangeException
matchCount;matchCount must be >= 1
IndexOfReverse(string, char, int, int, int)
Reports the index number, or character position, of the first occurrence of the specified Unicode character in the current String object. The search starts at a specified character position starting from the end and examines a specified number of character positions.
public static int IndexOfReverse(this string text, char charToFind, int startIndexFromEnd, int count, int matchCount = 1)
Parameters
textstringThe text.
charToFindcharThe character automatic find.
startIndexFromEndintThe starting index number for the search relative to the end of the string.
countintThe number of character positions to be examined.
matchCountintThe number of match before stopping. Default is 1
Returns
- int
The character position of the value parameter for the specified character if it is found, or -1 if it is not found.
Exceptions
- ArgumentNullException
text
- ArgumentOutOfRangeException
count;Count must be a positive value or startIndexFromEnd;StartIndexFromEnd must be a positive value or startIndexFromEnd;StartIndexFromEnd must be within the range of the string length or count;Count must be in the range of the string length minus the startIndexFromEnd
LastIndexOf(StringBuilder, char, int)
Calculates the last index of a char inside the following StringBuilder, equivalent of LastIndexOf(char) for a StringBuilder.
public static int LastIndexOf(this StringBuilder builder, char testChar, int startIndex = 0)
Parameters
builderStringBuilderThe builder.
testCharcharThe test character.
startIndexintThe start index.
Returns
- int
A position to the character found, or -1 if not found.
Exceptions
- ArgumentNullException
builder
SafeTrim(string?)
Safely trim a string.
public static string? SafeTrim(this string? value)
Parameters
valuestringThe string value. can be null
Returns
- string
The string trimmed.May be null if string was null
Substring(StringBuilder, int)
public static string Substring(this StringBuilder builder, int startIndex)
Parameters
builderStringBuilderstartIndexint
Returns
Substring(StringBuilder, int, int)
public static string Substring(this StringBuilder builder, int startIndex, int length)
Parameters
builderStringBuilderstartIndexintlengthint
Returns
ToFormat(string, params object?[])
Extension to format a string using Format(string, object) method by allowing to use it directly on a string.
public static string ToFormat(this string stringToFormat, params object?[] argumentsToFormat)
Parameters
stringToFormatstringThe string automatic format.
argumentsToFormatobject[]The arguments automatic format.
Returns
- string
A formatted string. See Format(string, object)