It is a pattern that occurs quite often in one part of our Framework.
Given an Array of Strings, we have to concat all of them, seperated by Semicolons.
I´d like to know in which elegant way it can be done.
I`ve seen some variations across our codebase, and always, when i have to do this, i have to rethink again.
My current pattern is this:
String[] values = new String[] {"a","b","c","d"};
String concat = String.Empty;
foreach(String s in values)
{
if(String.IsEmptyOrNullString(s) == false)
concat + = ", ";
concat += s;
}
What negs me is the if statement, i could insert the first item before the loop and start with a for loop, starting at index 1, but this doesn´t increase the readability.
What are your suggestions?
You can use
string.Join():This will result in something looking like this: