Previously, I would loop through every element of the array, append the delimiter and a space for readability, then go back, and chop off the last two characters. I never did like this, and always thought there may be a better way.
String[] Color ={"Red", "Orange", "Yellow", "Green", "Indigo", "Blue", "Violet"};
StringBuilder loopedColor = new StringBuilder();
foreach (var theColor in Color)
{
loopedColor.Append(theColor);
loopedColor.Append(", ");
}
var returnedLoop = loopedColor.ToString().Substring(0, loopedColor.ToString().Length - 2);
Later I would find the function that takes care of it for me- String.Join();
String[] Color ={"Red", "Orange", "Yellow", "Green", "Indigo", "Blue", "Violet"};
var JoinedStatement = String.Join(", ", Color);
Cleaner, and I'm sure Microsoft has thought about it more than I have. Why re-invent the wheel?
No comments:
Post a Comment