Class StringExtensions
- Namespace
- Garyon.Extensions
- Assembly
- Garyon.dll
Provides extension methods for the string class.
public static class StringExtensions
- Inheritance
-
StringExtensions
- Inherited Members
Methods
Combine(IEnumerable<string>)
Combines the strings of a string collection and returns the new string.
public static string Combine(this IEnumerable<string> strings)
Parameters
stringsIEnumerable<string>The collection of strings.
Returns
Combine(IEnumerable<string>, char)
Combines the strings of a string collection with a separator and returns the new string.
public static string Combine(this IEnumerable<string> strings, char separator)
Parameters
stringsIEnumerable<string>The collection of strings.
separatorcharThe separator of the strings.
Returns
Combine(IEnumerable<string>, string)
Combines the strings of a string collection with a separator and returns the new string.
public static string Combine(this IEnumerable<string> strings, string separator)
Parameters
stringsIEnumerable<string>The collection of strings.
separatorstringThe separator of the strings.
Returns
EnsureAffix(string?, string, AffixType)
Evaluates a string such that the returned result will contain the specified affix depending on the provided affix type, without concatenating it if it is already present.
public static string EnsureAffix(this string? source, string affix, AffixType affixType)
Parameters
sourcestringThe source string to which to guarantee an affix.
affixstringThe affix that will be contained in the resulting string.
affixTypeAffixTypeDetermines whether to ensure the affix at the start, the end, or both.
Returns
- string
The affix itself if the source string is null or empty, or the source string with the ensured affix.
EnsureEndsWith(string?, string)
Evaluates a string such that the returned result will end with the specified suffix, without concatenating it if it is already present.
public static string EnsureEndsWith(this string? source, string suffix)
Parameters
sourcestringThe source string to which to guarantee a suffix.
suffixstringThe suffix that will be contained in the resulting string.
Returns
- string
The suffix itself if the source string is null or empty, or the source string if it already ends with the suffix, otherwise a concatenation of the source string and the suffix.
EnsureStartsEndsWith(string?, string)
Evaluates a string such that the returned result will begin and end with the specified affix, without concatenating it if it is already present.
public static string EnsureStartsEndsWith(this string? source, string affix)
Parameters
sourcestringThe source string to which to guarantee an affix at the start and the end.
affixstringThe affix that will be contained in the resulting string.
Returns
EnsureStartsEndsWith(string?, string, string)
Evaluates a string such that the returned result will begin with the specified prefix and end with the specified suffix, without concatenating either if it is already present.
public static string EnsureStartsEndsWith(this string? source, string prefix, string suffix)
Parameters
sourcestringThe source string to which to guarantee a prefix and a suffix.
prefixstringThe prefix that will be contained in the resulting string.
suffixstringThe suffix that will be contained in the resulting string.
Returns
- string
A concatenation of the prefix and suffix if the source string is null or empty, or the source string with the ensured prefix and suffix.
EnsureStartsWith(string?, string)
Evaluates a string such that the returned result will begin with the specified prefix, without concatenating it if it is already present.
public static string EnsureStartsWith(this string? source, string prefix)
Parameters
sourcestringThe source string to which to guarantee a prefix.
prefixstringThe prefix that will be contained in the resulting string.
Returns
- string
The prefix itself if the source string is null or empty, or the source string if it already starts with the prefix, otherwise a concatenation of the prefix and the source string.
FindOccurrence(string, string, int)
Finds a substring within the string. Returns the index of the first character where the match occurred, otherwise -1.
public static int FindOccurrence(this string s, string match, int occurrence)
Parameters
sstringThe string within which the search will be performed.
matchstringThe substring to match from the original string.
occurrenceintThe 1-based index of the occurrence to find.
Returns
FindOccurrenceFromEnd(string, string, int)
Finds a substring within the string from the end. Returns the index of the first character where the match occurred, otherwise -1.
public static int FindOccurrenceFromEnd(this string s, string match, int occurrence)
Parameters
sstringThe string within which the search will be performed.
matchstringThe substring to match from the original string.
occurrenceintThe 1-based index of the occurrence to find.
Returns
IndexOfAfter(string, string)
Gets the index of the first character after the first occurrence of the given sequence in the original string.
public static int IndexOfAfter(this string source, string match)
Parameters
sourcestringThe original string from which to get the first occurrence.
matchstringThe matching sequence to find the first occurrence of.
Returns
- int
The index of the first character after the first occurrence of
matchinsource, if found, otherwise -1.
IndexOfAfter(string, string, int)
Gets the index of the first character after the first occurrence of the given sequence in the original string.
public static int IndexOfAfter(this string source, string match, int startIndex)
Parameters
sourcestringThe original string from which to get the first occurrence.
matchstringThe matching sequence to find the first occurrence of.
startIndexintThe index of the first character to include to the search, from which onwards the search will be performed.
Returns
- int
The index of the first character after the first occurrence of
matchinsource, if found, otherwise -1.
IndexOfAfter(string, string, int, int)
Gets the index of the first character after the first occurrence of the given sequence in the original string.
public static int IndexOfAfter(this string source, string match, int startIndex, int length)
Parameters
sourcestringThe original string from which to get the first occurrence.
matchstringThe matching sequence to find the first occurrence of.
startIndexintThe index of the first character to include to the search, from which onwards the search will be performed.
lengthintThe number of characters that will be searched, starting from the provided starting index.
Returns
- int
The index of the first character after the first occurrence of
matchinsource, if found, otherwise -1.
IsValidHexString(string)
Determines whether the string is a valid hex string.
public static bool IsValidHexString(this string hex)
Parameters
hexstringThe string.
Returns
- bool
true if the string is a valid hex string; that is, it only consists of valid hex characters, otherwise false.
NonEmpty(IEnumerable<string?>)
Gets all the non-null and non-empty strings from a collection.
public static IEnumerable<string> NonEmpty(this IEnumerable<string?> strings)
Parameters
stringsIEnumerable<string>The collection of strings.
Returns
NormalizeLineEndings(string)
Normalizes the line endings of a string to \n.
public static string NormalizeLineEndings(this string s)
Parameters
sstringThe string whose line endings to normalize.
Returns
- string
The string with normalized line endings.
NullIfEmpty(string)
public static string? NullIfEmpty(this string s)
Parameters
sstring
Returns
NullIfEmptyOrWhitespace(string)
public static string? NullIfEmptyOrWhitespace(this string s)
Parameters
sstring
Returns
ParseByte(string)
public static byte ParseByte(this string s)
Parameters
Returns
ParseDecimal(string)
public static decimal ParseDecimal(this string s)
Parameters
Returns
ParseDouble(string)
public static double ParseDouble(this string s)
Parameters
Returns
ParseInt16(string)
public static short ParseInt16(this string s)
Parameters
Returns
ParseInt32(string)
public static int ParseInt32(this string s)
Parameters
Returns
ParseInt64(string)
public static long ParseInt64(this string s)
Parameters
Returns
ParseNullableDouble(string)
public static double? ParseNullableDouble(this string str)
Parameters
strstring
Returns
ParseNullableInt32(string)
public static int? ParseNullableInt32(this string str)
Parameters
strstring
Returns
- int?
ParseNullableInt64(string)
public static long? ParseNullableInt64(this string str)
Parameters
strstring
Returns
- long?
ParseNullableSingle(string)
public static float? ParseNullableSingle(this string str)
Parameters
strstring
Returns
ParseNullableUInt32(string)
public static uint? ParseNullableUInt32(this string str)
Parameters
strstring
Returns
- uint?
ParseNullableUInt64(string)
public static ulong? ParseNullableUInt64(this string str)
Parameters
strstring
Returns
ParseSByte(string)
public static sbyte ParseSByte(this string s)
Parameters
Returns
ParseSingle(string)
public static float ParseSingle(this string s)
Parameters
Returns
ParseUInt16(string)
public static ushort ParseUInt16(this string s)
Parameters
Returns
ParseUInt32(string)
public static uint ParseUInt32(this string s)
Parameters
Returns
ParseUInt64(string)
public static ulong ParseUInt64(this string s)
Parameters
Returns
RemoveCharacterRepetitions(string)
Returns a string that removes repetitions of the same character.
Example:
RemoveCharacterRepetitions("aabcc") = "abc"
public static string RemoveCharacterRepetitions(this string s)
Parameters
sstringThe string to remove the character repetitions from.
Returns
RemoveLast(string)
Removes one character from the end of the string.
public static string RemoveLast(this string s)
Parameters
sstringThe string whose last character to remove.
Returns
- string
The string without the removed last character.
RemoveLast(string, int)
Removes a number of characters from the end of the string.
public static string RemoveLast(this string s, int characters)
Parameters
sstringThe string whose last characters to remove.
charactersintThe number of characters to remove.
Returns
- string
The string without the removed last characters.
Repeat(string, int)
Repeats a string a number of times.
public static string Repeat(this string s, int count)
Parameters
sstringThe string to repeat the specified number of times.
countintThe number of times to repeat the string. The value cannot be negative.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
countis negative.
Replace(string, string, int, int)
Replaces a part of the string with a new one.
public static string Replace(this string originalString, string replacedString, int startIndex, int length)
Parameters
originalStringstringThe original string.
replacedStringstringThe new string to replace to the part of the original one.
startIndexintThe starting index of the substring to replace.
lengthintThe length of the substring to replace.
Returns
ReplaceWholeWord(string, string, string)
Replaces a whole word of the original string and returns the new one.
public static string ReplaceWholeWord(this string originalString, string oldString, string newString)
Parameters
originalStringstringThe original string which will be replaced.
oldStringstringThe old part of the string in the original string to replace.
newStringstringThe new part of the string which will be contained in the returned string.
Returns
Substring(string, string, string)
Returns a substring that begins with the beginning matched string and ends before the ending matched string.
public static string Substring(this string s, string from, string to)
Parameters
sstringThe string whose substring to return.
fromstringThe beginning matching string to begin the substring from.
tostringThe ending matching string to end the substring at.
Returns
SubstringUntilFirst(string, char)
public static string SubstringUntilFirst(this string s, char match)
Parameters
Returns
SubstringUntilFirst(string, string)
public static string SubstringUntilFirst(this string s, string match)
Parameters
Returns
SubstringUntilLast(string, char)
public static string SubstringUntilLast(this string s, char match)
Parameters
Returns
SubstringUntilLast(string, string)
public static string SubstringUntilLast(this string s, string match)
Parameters
Returns
TryParseByte(string, out byte)
public static bool TryParseByte(this string s, out byte value)
Parameters
sstringThe string to try to parse.
valuebyteThe byte value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseDecimal(string, out decimal)
public static bool TryParseDecimal(this string s, out decimal value)
Parameters
sstringThe string to try to parse.
valuedecimalThe decimal value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseDouble(string, out double)
public static bool TryParseDouble(this string s, out double value)
Parameters
sstringThe string to try to parse.
valuedoubleThe double value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseInt16(string, out short)
public static bool TryParseInt16(this string s, out short value)
Parameters
sstringThe string to try to parse.
valueshortThe short value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseInt32(string, out int)
public static bool TryParseInt32(this string s, out int value)
Parameters
sstringThe string to try to parse.
valueintThe int value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseInt64(string, out long)
public static bool TryParseInt64(this string s, out long value)
Parameters
sstringThe string to try to parse.
valuelongThe long value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseSByte(string, out sbyte)
public static bool TryParseSByte(this string s, out sbyte value)
Parameters
sstringThe string to try to parse.
valuesbyteThe sbyte value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseSingle(string, out float)
public static bool TryParseSingle(this string s, out float value)
Parameters
sstringThe string to try to parse.
valuefloatThe float value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseUInt16(string, out ushort)
public static bool TryParseUInt16(this string s, out ushort value)
Parameters
sstringThe string to try to parse.
valueushortThe ushort value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseUInt32(string, out uint)
public static bool TryParseUInt32(this string s, out uint value)
Parameters
sstringThe string to try to parse.
valueuintThe uint value of the string, if the parsing was successful, otherwise 0.
Returns
TryParseUInt64(string, out ulong)
public static bool TryParseUInt64(this string s, out ulong value)
Parameters
sstringThe string to try to parse.
valueulongThe ulong value of the string, if the parsing was successful, otherwise 0.