Add string.AsNullIfEmpty and string.AsNullIfWhiteSpace methods

This commit is contained in:
Oliver Booth 2020-10-20 14:58:32 +01:00
parent 00acaff779
commit 6a1f05832e
1 changed files with 21 additions and 0 deletions

View File

@ -12,6 +12,27 @@
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Returns the current string, or <see langword="null" /> if the current string is null or empty.
/// </summary>
/// <param name="value">The value to sanitize.</param>
/// <returns><see langword="null" /> or <paramref name="value" />.</returns>
public static string? AsNullIfEmpty(this string value)
{
return string.IsNullOrEmpty(value) ? null : value;
}
/// <summary>
/// Returns the current string, or <see langword="null" /> if the current string is null, empty, or consists of only
/// whitespace.
/// </summary>
/// <param name="value">The value to sanitize.</param>
/// <returns><see langword="null" /> or <paramref name="value" />.</returns>
public static string? AsNullIfWhiteSpace(this string value)
{
return string.IsNullOrWhiteSpace(value) ? null : value;
}
/// <summary>
/// Decodes a base-64 encoded string.
/// </summary>