public static string JoinWithSemiColon <T>(this IEnumerable <T> values, StringJoinOptions options = StringJoinOptions.None) { var suffix = GetStringJoinSeparatorSuffix(options); return(values.JoinWith(";".Append(suffix))); }
private static string GetStringJoinSeparatorSuffix(StringJoinOptions options) { var shouldAddSpace = (options & StringJoinOptions.AddSpaceSuffix) == StringJoinOptions.AddSpaceSuffix; return(shouldAddSpace ? " " : string.Empty); }
/// <summary> /// Concatenates the members of a <see cref="IEnumerable{T}"/> collection built of <see cref="string"/> type, using the separator specified between each member, and applies the specified behavior for the null, empty or WhiteSpace entries. /// </summary> /// <param name="values">Collection that contains the chains to be concatenated.</param> /// <param name="separator">String to use as separator. Separator is included in the returned string only if values contains several elements.</param> /// <param name="joinOptions">Behaviour to be use for the null, Empty or WhiteSpace entries.</param> /// <returns>String composed of the value elements delimited by the separator. If values is an empty array, the method returns <see cref="string.Empty"/>.</returns> static public string Join(this IEnumerable <string> values, string separator, StringJoinOptions joinOptions) { if (values == null || values.Count() == 0) { return(string.Empty); } if (values.Count() == 1) { string first = values.First(); if (first != null) { return(first); } else { return(string.Empty); } } if (separator == null) { separator = string.Empty; } if (separator == null) { separator = string.Empty; } List <string> rslt = new List <string>(values.Count()); foreach (string item in values) { switch (joinOptions) { case StringJoinOptions.SkipNull: if (item != null) { rslt.Add(item); } break; case StringJoinOptions.NullToNull: if (item == null) { rslt.Add("Null"); } else { rslt.Add(item); } break; case StringJoinOptions.SkipNullAndEmpty: if (!item.IsNullOrEmpty()) { rslt.Add(item); } break; case StringJoinOptions.SkipNullAndWhiteSpace: if (!item.IsNullOrWhiteSpace()) { rslt.Add(item); } break; default: //StringOneLineOptions.NullToEmpty if (item == null) { rslt.Add(string.Empty); } else { rslt.Add(item); } break; } } return(string.Join(separator, rslt)); }
/// <summary> /// Concatenates the members of a <see cref="IEnumerable{T}"/> collection, using the separator specified between each member, and applies the specified behavior for the null, empty or WhiteSpace entries. /// </summary> /// <param name="values">Collection that contains the chains to be concatenated.</param> /// <param name="separator">String to use as separator. Separator is included in the returned string only if values contains several elements.</param> /// <param name="joinOptions">Behaviour to be use for the null, Empty or WhiteSpace entries.</param> /// <returns>String composed of the value elements delimited by the separator. If values is an empty array, the method returns <see cref="string.Empty"/>.</returns> static public string ToOneString <T>(this IEnumerable <T> values, string separator, StringJoinOptions joinOptions) { if (values == null) { return(string.Empty); } return(values.ToStringEnumerable().Join(separator, joinOptions)); }