Skip to content

Commit

Permalink
Adds string extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Desz01ate committed Aug 3, 2023
1 parent b0a2a2c commit 4e6b6cf
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Codehard.Functional/Codehard.Functional/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Diagnostics.Contracts;

namespace Codehard.Functional;

/// <summary>
/// Provides extension methods for a string.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Returns the original string if it's not null or empty, otherwise returns the alternative value.
/// </summary>
[Pure]
public static string IfNullOrEmpty(this string s, string alternative)
=> string.IsNullOrEmpty(s) ? alternative : s;

/// <summary>
/// Returns the original string if it's not null or whitespace, otherwise returns the alternative value.
/// </summary>
[Pure]
public static string IfNullOrWhitespace(this string s, string alternative)
=> string.IsNullOrWhiteSpace(s) ? alternative : s;
}

0 comments on commit 4e6b6cf

Please sign in to comment.