List output with Console.Write
Hello, i try to output a Fibonacci list with ';' as separator. I don't want ';' after the last item, so i make it in a quite complicated way.
any suggestions, please ?
3 Replies
well anyway, getting fb last value with index ugly and slow (as recomputed at each iteration) so at least i can propose:
Right, so first, you should declare
var sep = "; "
outside of the loop, since it doesn't change. Other than that you figured out a way, which is good
But now you can upgrade:
string.Join("; ", fb)
will give you the string you want
Where the first argument of string.Join
is the delimiter you want to use and the second argument is any enumerable (lists, collections, etc.)
Also a hint for the future: If there's no handy function like string.Join, you might wanna use the item index to determine if you are at the end of an array. This does not run the risk of recomputing the last element or running an overly complicated comparison if the list contains objectsah nice concatenation function ! I will now explore string class, to see what it offers. Thank you for the help.