From 1601084cb4f5c8be6a60a2868aca8ff79aaf9bd0 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 20 Oct 2020 14:58:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20string.WithAlternative?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- X10D/src/StringExtensions.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/X10D/src/StringExtensions.cs b/X10D/src/StringExtensions.cs index 50a80a8..d722cd2 100644 --- a/X10D/src/StringExtensions.cs +++ b/X10D/src/StringExtensions.cs @@ -373,5 +373,27 @@ return TimeSpanParser.Parse(str); } + + /// + /// Returns the current string, or an alternative value if the current string is null or empty, or optionally if the + /// current string consists of only whitespace. + /// + /// The value to check. + /// The alternative value if does not meet the criteria. + /// + /// Optional. If set to , will be returned also if + /// only consists of whitespace. + /// + /// Returns or . + /// is . + public static string WithAlternative(this string? value, string alternative, bool includeWhiteSpace = false) + { + if (alternative is null) + { + throw new ArgumentNullException(nameof(alternative)); + } + + return (includeWhiteSpace ? value?.AsNullIfWhiteSpace() : value?.AsNullIfEmpty()) ?? alternative; + } } }