Skip to content

Instantly share code, notes, and snippets.

@jehugaleahsa
Created October 9, 2018 14:29
Show Gist options
  • Save jehugaleahsa/ae0f44688c5895582f7b8ba31980f190 to your computer and use it in GitHub Desktop.
Save jehugaleahsa/ae0f44688c5895582f7b8ba31980f190 to your computer and use it in GitHub Desktop.
Join with And
public static string Join<TItem>(IEnumerable<TItem> items, string separator = ", ", string end = " and ")
{
var builder = new StringBuilder();
using (var enumerator = items.GetEnumerator())
{
if (enumerator.MoveNext())
{
var item = enumerator.Current;
if (enumerator.MoveNext())
{
builder.Append(item);
item = enumerator.Current;
while (enumerator.MoveNext())
{
builder.Append(separator);
builder.Append(item);
item = enumerator.Current;
}
builder.Append(end);
}
builder.Append(item);
}
}
return builder.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment