Skip to content

Commit

Permalink
feat(utilities): add string extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoch committed Feb 22, 2024
1 parent f76db12 commit e2c7e08
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
22 changes: 21 additions & 1 deletion source/Nuke.Utilities/Text/String.Emptiness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,32 @@ public static bool IsNullOrEmpty(this string str)
}

/// <summary>
/// Indicates whether a specified string is null, empty, or consists only of white-space characters.
/// Indicates whether a specified string is null, empty, or only white-space.
/// </summary>
[Pure]
[ContractAnnotation("null => halt")]
public static bool IsNullOrWhiteSpace(this string str)
{
return string.IsNullOrWhiteSpace(str);
}

/// <summary>
/// Returns <value>null</value> if the specified string is empty.
/// </summary>
[Pure]
[ContractAnnotation("null => null")]
public static string ToNullIfEmpty(this string str)
{
return str.IsNullOrEmpty() ? null : str;
}

/// <summary>
/// Returns <value>null</value> if the specified string is empty or only white-space.
/// </summary>
[Pure]
[ContractAnnotation("null => null")]
public static string ToNullIfWhiteSpace(this string str)
{
return str.IsNullOrWhiteSpace() ? null : str;
}
}
13 changes: 11 additions & 2 deletions source/Nuke.Utilities/Text/String.Split.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ public static IEnumerable<string> SplitCamelHumps(this string str, params string
/// Splits a given string by new-lines with empty entries preserved.
/// </summary>
[Pure]
public static string[] SplitLineBreaks(this string str)
public static string[] SplitLineBreaks(this string str, StringSplitOptions options = StringSplitOptions.None)
{
return str.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
return str.Split(new[] { "\r\n", "\n" }, options);
}

/// <summary>
/// Splits a given string by paragraphs (double new-line) with empty entries preserved.
/// </summary>
[Pure]
public static string[] SplitParagraphs(this string str, StringSplitOptions options = StringSplitOptions.None)
{
return str.Split(new[] { "\r\n\r\n", "\n\n" }, options);
}

/// <summary>
Expand Down

0 comments on commit e2c7e08

Please sign in to comment.