The idea was good, but I think we could probably do something like below:
public static string UsingStringJoin(IEnumerable<string> sList, string separator)
{
return sList.Any() ? string.Join(separator, sList.ToArray()) : string.Empty;
}
public static string UsingStringBuilder(IEnumerable<string> sList, string separator)
{
return sList.Any() ? BuildString(sList, seperator) : string.Empty;
}
public static string BuildString(IEnumerable<string> sList, string separator)
{
StringBuilder builder = new StringBuilder();
foreach (string item in sList)
builder.Append(string.Concat(item, separator));
string buildedString = builder.ToString();
return buildedString.Remove(buildedString.Length - 1);
}
If we have few
string
s to merge, probably use
string.Concat(...)
or
string.Join(...)
or for larger set of
string
s
StringBuilder
. Certainly your idea is good, I just want to show an alternate way to do the thing.