Created
October 9, 2018 14:29
-
-
Save jehugaleahsa/ae0f44688c5895582f7b8ba31980f190 to your computer and use it in GitHub Desktop.
Join with And
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
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