namespace MetaNodes;
///
/// Extension methods
///
internal static class ExtensionMethods
{
///
/// Returns an empty string as null, otherwise returns the original string
///
/// the input string
/// the string or null if empty
public static string? EmptyAsNull(this string str)
{
return str == string.Empty ? null : str;
}
///
/// Trims the specified characters from the beginning and end of the string.
///
/// The input string to trim.
/// The characters to trim from the string.
/// A new string that has the specified characters removed from the beginning and end.
/// Thrown when input is null.
public static string TrimExtra(this string input, string charsToTrim)
{
if (input == null)
throw new ArgumentNullException(nameof(input));
if (string.IsNullOrEmpty(charsToTrim))
return input.Trim();
int startIndex = 0;
int endIndex = input.Length - 1;
while (startIndex <= endIndex && (input[startIndex] == ' ' || charsToTrim.Contains(input[startIndex])))
{
startIndex++;
}
while (endIndex >= startIndex && (input[endIndex] == ' ' || charsToTrim.Contains(input[endIndex])))
{
endIndex--;
}
return input.Substring(startIndex, endIndex - startIndex + 1);
}
}