Last active
December 11, 2021 17:59
-
-
Save chrisfcarroll/12698416ce88d634ba9c07e1e8820a81 to your computer and use it in GitHub Desktop.
Extended alternatives for <c>string.Join(…)</c> methods to omit null, empty, or whitespace-only items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary>Extended alternatives for <c>string.</c> methods</summary> | |
// ReSharper disable once InconsistentNaming | |
public static class strings | |
{ | |
/// <summary>Joins the non-null items of <paramref name="values"/>, putting <paramref name="separator"/> | |
/// between each item.</summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-null elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of <c>value.ToString()</c> for each non-null value in <paramref name="values"/>, | |
/// with <paramref name="separator"/> between each one. | |
/// If values is null or has no non-null items, the method returns Empty.</returns> | |
public static string JoinNonNull<T>(string separator, IEnumerable<T>? values) | |
=> values is null ? "" : string.Join(separator, values!.Where(v => v != null)); | |
/// <summary>Joins the non-null items of <paramref name="values"/>, putting <paramref name="separator"/> | |
/// between each item.</summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-null elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of <c>value.ToString()</c> for each non-null value in <paramref name="values"/>, | |
/// with <paramref name="separator"/> between each one. | |
/// If values is null or has no non-null items, the method returns Empty.</returns> | |
public static string JoinNonNull<T>(char separator, IEnumerable<T>? values) | |
=> values is null ? "" : string.Join(separator, values!.Where(v => v != null)); | |
/// <summary>Joins the items of <paramref name="values"/>, putting <paramref name="separator"/> between | |
/// each item, but omitting any item whose <see cref="object.ToString"/> returns either <c>null</c> | |
/// or an empty string. </summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-empty elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of <c>value.ToString()</c> for each non-empty value in <paramref name="values"/>, | |
/// with <paramref name="separator"/> between each one. | |
/// If values is null or has no non-empty items, the method returns Empty.</returns> | |
public static string JoinNonEmpty<T>(string separator, IEnumerable<T>? values) | |
=> values is null ? "" | |
: string.Join(separator, | |
values!.Where(v => v != null) | |
.Select(v=>v!.ToString()).Where(v=>!string.IsNullOrEmpty(v))); | |
/// <summary>Joins the non-null items of <paramref name="values"/>, putting <paramref name="separator"/> | |
/// between each item.</summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-empty elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of <c>value.ToString()</c> for each non-empty value in <paramref name="values"/>, | |
/// with <paramref name="separator"/> between each one. | |
/// If values is null or has no non-empty items, the method returns Empty.</returns> | |
public static string JoinNonEmpty<T>(char separator, IEnumerable<T>? values) | |
=> values is null ? "" | |
: string.Join(separator, values?.Where(v => v != null) | |
.Select(v=>v.ToString()).Where(v=>!string.IsNullOrEmpty(v))); | |
/// <summary>Joins the non-null items of <paramref name="values"/>, putting <paramref name="separator"/> | |
/// between each item. </summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-null elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of each non-null item of of <paramref name="values"/> with | |
/// <paramref name="separator"/> between each one. | |
/// If values has no non-null items, the method returns Empty.</returns> | |
public static string JoinNonNull(string separator, params string?[] values) | |
=> string.Join(separator, values?.Where(v => v != null)); | |
/// <summary>Joins the non-null items of <paramref name="values"/>, putting <paramref name="separator"/> | |
/// between each item. </summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-null elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of each non-null item of of <paramref name="values"/> with | |
/// <paramref name="separator"/> between each one. | |
/// If values has no non-null items, the method returns Empty.</returns> | |
public static string JoinNonNull(char separator, params string?[] values) | |
=> string.Join(separator, values.Where(v => v != null)); | |
/// <summary>Joins the items of <paramref name="values"/>, putting <paramref name="separator"/> between each item | |
/// but omitting any item whose <see cref="object.ToString"/> returns either <c>null</c> or an empty string. | |
/// </summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-empty elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of <c>value.ToString()</c> for each non-empty value in <paramref name="values"/>, | |
/// with <paramref name="separator"/> between each one. | |
/// If values has no non-empty items, the method returns Empty.</returns> | |
public static string JoinNonEmpty(string separator, params string?[] values) | |
=> string.Join(separator, values.Where(v=>!string.IsNullOrEmpty(v))); | |
/// <summary>Joins the items of <paramref name="values"/>, putting <paramref name="separator"/> between each item | |
/// but omitting any item whose <see cref="object.ToString"/> returns either <c>null</c> or an empty string. | |
/// </summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-empty elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of <c>value.ToString()</c> for each non-empty value in <paramref name="values"/>, | |
/// with <paramref name="separator"/> between each one. | |
/// If values has no non-empty items, the method returns Empty.</returns> | |
public static string JoinNonEmpty(char separator, params string?[] values) | |
=> string.Join(separator, values.Where(v=>!string.IsNullOrEmpty(v))); | |
/// <summary>Joins the items of <paramref name="values"/>, putting <paramref name="separator"/> between | |
/// each item but omitting any item whose <see cref="object.ToString"/> which are null, empty, or | |
/// whitespace only.</summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-empty elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of <c>value.ToString()</c> for each non-empty value in <paramref name="values"/>, | |
/// with <paramref name="separator"/> between each one. | |
/// If values has no non-empty items, the method returns Empty.</returns> | |
public static string JoinNonEmptyOrWhitespace(string separator, params string?[] values) | |
=> string.Join(separator, values.Where(v => !string.IsNullOrWhiteSpace(v))); | |
/// <summary>Joins the items of <paramref name="values"/>, putting <paramref name="separator"/> between | |
/// each item but omitting any item whose <see cref="object.ToString"/> which are null, empty, or | |
/// whitespace only.</summary> | |
/// <param name="separator">Separator is only used if <paramref name="values"/> has at least | |
/// 2 non-empty elements.</param> | |
/// <param name="values">The values to join.</param> | |
/// <typeparam name="T"></typeparam> | |
/// <returns>A string of <c>value.ToString()</c> for each non-empty value in <paramref name="values"/>, | |
/// with <paramref name="separator"/> between each one. | |
/// If values has no non-empty items, the method returns Empty.</returns> | |
public static string JoinNonEmptyOrWhitespace(char separator, params string?[] values) | |
=> string.Join(separator, values.Where(v => !string.IsNullOrWhiteSpace(v))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment