Created
October 11, 2021 13:19
-
-
Save rianjs/ea6a3f51ee051fb144604a59fa9ee776 to your computer and use it in GitHub Desktop.
Joining strings while ignoring empty or null values
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 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