Table of Contents

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

strings IEnumerable<string>

The collection of strings.

Returns

string

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

strings IEnumerable<string>

The collection of strings.

separator char

The separator of the strings.

Returns

string

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

strings IEnumerable<string>

The collection of strings.

separator string

The separator of the strings.

Returns

string

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

source string

The source string to which to guarantee an affix.

affix string

The affix that will be contained in the resulting string.

affixType AffixType

Determines 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

source string

The source string to which to guarantee a suffix.

suffix string

The 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

source string

The source string to which to guarantee an affix at the start and the end.

affix string

The affix that will be contained in the resulting string.

Returns

string

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

source string

The source string to which to guarantee a prefix and a suffix.

prefix string

The prefix that will be contained in the resulting string.

suffix string

The 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

source string

The source string to which to guarantee a prefix.

prefix string

The 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

s string

The string within which the search will be performed.

match string

The substring to match from the original string.

occurrence int

The 1-based index of the occurrence to find.

Returns

int

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

s string

The string within which the search will be performed.

match string

The substring to match from the original string.

occurrence int

The 1-based index of the occurrence to find.

Returns

int

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

source string

The original string from which to get the first occurrence.

match string

The matching sequence to find the first occurrence of.

Returns

int

The index of the first character after the first occurrence of match in source, 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

source string

The original string from which to get the first occurrence.

match string

The matching sequence to find the first occurrence of.

startIndex int

The 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 match in source, 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

source string

The original string from which to get the first occurrence.

match string

The matching sequence to find the first occurrence of.

startIndex int

The index of the first character to include to the search, from which onwards the search will be performed.

length int

The 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 match in source, if found, otherwise -1.

IsValidHexString(string)

Determines whether the string is a valid hex string.

public static bool IsValidHexString(this string hex)

Parameters

hex string

The 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

strings IEnumerable<string>

The collection of strings.

Returns

IEnumerable<string>

NormalizeLineEndings(string)

Normalizes the line endings of a string to \n.

public static string NormalizeLineEndings(this string s)

Parameters

s string

The string whose line endings to normalize.

Returns

string

The string with normalized line endings.

NullIfEmpty(string)

public static string? NullIfEmpty(this string s)

Parameters

s string

Returns

string

NullIfEmptyOrWhitespace(string)

public static string? NullIfEmptyOrWhitespace(this string s)

Parameters

s string

Returns

string

ParseByte(string)

Parses the byte value of the specified string.

public static byte ParseByte(this string s)

Parameters

s string

The string to parse.

Returns

byte

The byte value of the string.

ParseDecimal(string)

Parses the decimal value of the specified string.

public static decimal ParseDecimal(this string s)

Parameters

s string

The string to parse.

Returns

decimal

The decimal value of the string.

ParseDouble(string)

Parses the double value of the specified string.

public static double ParseDouble(this string s)

Parameters

s string

The string to parse.

Returns

double

The double value of the string.

ParseInt16(string)

Parses the short value of the specified string.

public static short ParseInt16(this string s)

Parameters

s string

The string to parse.

Returns

short

The short value of the string.

ParseInt32(string)

Parses the int value of the specified string.

public static int ParseInt32(this string s)

Parameters

s string

The string to parse.

Returns

int

The int value of the string.

ParseInt64(string)

Parses the long value of the specified string.

public static long ParseInt64(this string s)

Parameters

s string

The string to parse.

Returns

long

The long value of the string.

ParseNullableDouble(string)

public static double? ParseNullableDouble(this string str)

Parameters

str string

Returns

double?

ParseNullableInt32(string)

public static int? ParseNullableInt32(this string str)

Parameters

str string

Returns

int?

ParseNullableInt64(string)

public static long? ParseNullableInt64(this string str)

Parameters

str string

Returns

long?

ParseNullableSingle(string)

public static float? ParseNullableSingle(this string str)

Parameters

str string

Returns

float?

ParseNullableUInt32(string)

public static uint? ParseNullableUInt32(this string str)

Parameters

str string

Returns

uint?

ParseNullableUInt64(string)

public static ulong? ParseNullableUInt64(this string str)

Parameters

str string

Returns

ulong?

ParseSByte(string)

Parses the sbyte value of the specified string.

public static sbyte ParseSByte(this string s)

Parameters

s string

The string to parse.

Returns

sbyte

The sbyte value of the string.

ParseSingle(string)

Parses the float value of the specified string.

public static float ParseSingle(this string s)

Parameters

s string

The string to parse.

Returns

float

The float value of the string.

ParseUInt16(string)

Parses the ushort value of the specified string.

public static ushort ParseUInt16(this string s)

Parameters

s string

The string to parse.

Returns

ushort

The ushort value of the string.

ParseUInt32(string)

Parses the uint value of the specified string.

public static uint ParseUInt32(this string s)

Parameters

s string

The string to parse.

Returns

uint

The uint value of the string.

ParseUInt64(string)

Parses the ulong value of the specified string.

public static ulong ParseUInt64(this string s)

Parameters

s string

The string to parse.

Returns

ulong

The ulong value of the string.

RemoveCharacterRepetitions(string)

Returns a string that removes repetitions of the same character.
Example: 
RemoveCharacterRepetitions("aabcc") = "abc"
public static string RemoveCharacterRepetitions(this string s)

Parameters

s string

The string to remove the character repetitions from.

Returns

string

RemoveLast(string)

Removes one character from the end of the string.

public static string RemoveLast(this string s)

Parameters

s string

The 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

s string

The string whose last characters to remove.

characters int

The 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

s string

The string to repeat the specified number of times.

count int

The number of times to repeat the string. The value cannot be negative.

Returns

string

The given string repeated the specified number of times. If count is 0, Empty is returned.

Exceptions

ArgumentOutOfRangeException

Thrown when count is 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

originalString string

The original string.

replacedString string

The new string to replace to the part of the original one.

startIndex int

The starting index of the substring to replace.

length int

The length of the substring to replace.

Returns

string

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

originalString string

The original string which will be replaced.

oldString string

The old part of the string in the original string to replace.

newString string

The new part of the string which will be contained in the returned string.

Returns

string

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

s string

The string whose substring to return.

from string

The beginning matching string to begin the substring from.

to string

The ending matching string to end the substring at.

Returns

string

SubstringUntilFirst(string, char)

public static string SubstringUntilFirst(this string s, char match)

Parameters

s string
match char

Returns

string

SubstringUntilFirst(string, string)

public static string SubstringUntilFirst(this string s, string match)

Parameters

s string
match string

Returns

string

SubstringUntilLast(string, char)

public static string SubstringUntilLast(this string s, char match)

Parameters

s string
match char

Returns

string

SubstringUntilLast(string, string)

public static string SubstringUntilLast(this string s, string match)

Parameters

s string
match string

Returns

string

TryParseByte(string, out byte)

Tries to parse the byte value of the specified string.

public static bool TryParseByte(this string s, out byte value)

Parameters

s string

The string to try to parse.

value byte

The byte value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseDecimal(string, out decimal)

Tries to parse the decimal value of the specified string.

public static bool TryParseDecimal(this string s, out decimal value)

Parameters

s string

The string to try to parse.

value decimal

The decimal value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseDouble(string, out double)

Tries to parse the double value of the specified string.

public static bool TryParseDouble(this string s, out double value)

Parameters

s string

The string to try to parse.

value double

The double value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseInt16(string, out short)

Tries to parse the short value of the specified string.

public static bool TryParseInt16(this string s, out short value)

Parameters

s string

The string to try to parse.

value short

The short value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseInt32(string, out int)

Tries to parse the int value of the specified string.

public static bool TryParseInt32(this string s, out int value)

Parameters

s string

The string to try to parse.

value int

The int value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseInt64(string, out long)

Tries to parse the long value of the specified string.

public static bool TryParseInt64(this string s, out long value)

Parameters

s string

The string to try to parse.

value long

The long value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseSByte(string, out sbyte)

Tries to parse the sbyte value of the specified string.

public static bool TryParseSByte(this string s, out sbyte value)

Parameters

s string

The string to try to parse.

value sbyte

The sbyte value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseSingle(string, out float)

Tries to parse the float value of the specified string.

public static bool TryParseSingle(this string s, out float value)

Parameters

s string

The string to try to parse.

value float

The float value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseUInt16(string, out ushort)

Tries to parse the ushort value of the specified string.

public static bool TryParseUInt16(this string s, out ushort value)

Parameters

s string

The string to try to parse.

value ushort

The ushort value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseUInt32(string, out uint)

Tries to parse the uint value of the specified string.

public static bool TryParseUInt32(this string s, out uint value)

Parameters

s string

The string to try to parse.

value uint

The uint value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.

TryParseUInt64(string, out ulong)

Tries to parse the ulong value of the specified string.

public static bool TryParseUInt64(this string s, out ulong value)

Parameters

s string

The string to try to parse.

value ulong

The ulong value of the string, if the parsing was successful, otherwise 0.

Returns

bool

true if the parsing was successful, otherwise false.