Skip to content

Instantly share code, notes, and snippets.

@rianjs
Created October 11, 2021 13:19
Show Gist options
  • Save rianjs/ea6a3f51ee051fb144604a59fa9ee776 to your computer and use it in GitHub Desktop.
Save rianjs/ea6a3f51ee051fb144604a59fa9ee776 to your computer and use it in GitHub Desktop.
Joining strings while ignoring empty or null values
public static string JoinStrings(IEnumerable<string> args, string separator)
{
var needsSeparator = false;
var builder = new StringBuilder();
foreach (var arg in args)
{
if (string.IsNullOrWhiteSpace(arg))
{
continue;
}
if (needsSeparator)
{
builder.Append(separator);
}
builder.Append(arg);
needsSeparator = true;
}
return builder.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment