Lately, I'm often faced to check if a given string is null, empty or only white space. You all know the built-in method string.IsNullOrEmpty(string). However when writting code, for me, it feels quite juddery to use a static type method to check that. So I decided to give an extension method so I can just write stringVariable.IsNullOrEmpty(). You may think: "How can you execute a member method on a null-reference?". This is possible, because all extended methods are technically seen, static methods - they just pretend to be member methods.
And when I do variable checks, I like to use the null-coalescing operator (the double question mark ??). However, this is not possible when the string is just empty or whitespace. To solve that, I wrote also some extended methods, so you can just use stringVariable.EmptyToNull() or stringVariable.WhiteSpaceToNull(). Both just straighten a given value to null, if it equals empty or white space.
using System;
using System.Globalization;
namespace Luegisdorf
{
/// <summary>
/// Provides extended functionality for instances of type string
/// </summary>
public static class StringExtensions
{
/// <inheritdoc cref="string.IsNullOrEmpty(string)"/>
public static bool IsNullOrEmpty(this string value)
=> string.IsNullOrEmpty(value);
/// <inheritdoc cref="string.IsNullOrWhiteSpace(string)"/>
public static bool IsNullOrWhiteSpace(this string value)
=> string.IsNullOrWhiteSpace(value);
/// <summary>
/// Straights string to <c>null</c>, if given instance equals to empty string.
/// </summary>
/// <remarks>
/// Can mostly used to null-straighten a string when using validation
/// with null-coalescing operator.
/// </remarks>
/// <param name="instance">
/// The instance to straighten
/// </param>
/// <returns>
/// <c>Null</c>, if the given <paramref>instance</paramref> is already <c>null</c>
/// or equals empty string,
/// otherwise the given <c>instance</c>.
/// </returns>
/// <example>
/// Inline usage:
/// <code>
/// var candidate = string.Empty;
/// var length = (candidate.EmptyToNull()
/// ?? throw new Exception("Candidate must contain value")).Length;
/// </code>
///
/// If condition usage:
/// <code>
/// var candidate = string.Empty;
/// if ((candidate.EmptyToNull()
/// ?? throw new Exception("Candidate must contain value"))
/// is string properString)
/// {
/// var length = properString.Length;
/// var firstCharacter = properString[0];
/// }
/// </code>
/// </example>
public static string EmptyToNull(this string instance)
{
string ret = instance;
if (string.IsNullOrEmpty(instance))
{
ret = null;
}
return ret;
}
/// <summary>
/// Straights string to <c>null</c>, if given instance equals to empty string.
/// </summary>
/// <remarks>
/// Can mostly used to null-straighten a string when using validation
/// with null-coalescing operator.
/// </remarks>
/// <param name="instance">
/// The instance to straighten
/// </param>
/// <returns>
/// <c>Null</c>, if the given <paramref>instance</paramref> is already <c>null</c>
/// or equals white space,
/// otherwise the given <c>instance</c>.
/// </returns>
/// <example>
/// Inline usage:
/// <code>
/// var candidate = string.Empty;
/// var length = (candidate.WhiteSpaceToNull()
/// ?? throw new Exception("Candidate must contain value")).Length;
/// </code>
///
/// If condition usage:
/// <code>
/// var candidate = string.Empty;
/// if ((candidate.WhiteSpaceToNull()
/// ?? throw new Exception("Candidate must contain value"))
/// is string properString)
/// {
/// var length = properString.Length;
/// var firstCharacter = properString[0];
/// }
/// </code>
/// </example>
public static string WhiteSpaceToNull(this string instance)
{
string ret = instance;
if (string.IsNullOrWhiteSpace(instance))
{
ret = null;
}
return ret;
}
}
}
No comments:
Post a Comment