Putting a separator between your elements
16.04.09 / Programming / Author: astrobunny
Tags: better way, C++, separator, string.join
Often when I’m programming, I’ll need to list a bunch of stuff and put commas between them. Like this:
1,2,3,4,5
Usually, this is what I, and most of my colleagues do:
List<int> nums = new List<int>(); for (int i = 0; i < 5; i++) { nums.Add(i); } string msg = ""; bool first = true; foreach (int num in nums) { if (first) { first = false; } else { msg += ","; } msg += num.ToString(); }
After reading Eric Lippert’s little article about the horrid-seeming problem of commas and lists, I noticed him mentioning the method
String.Join
.
Needless to say, I went and tried it, and found that I could do the equivalent of the above, like this:
List<int> nums = new List<int>(); for (int i = 0; i < 5; i++) { nums.Add(i); } string msg = string.Join(",", nums.Select(x => x.ToString()).ToArray()); Console.WriteLine(msg);
Its kinda amazing how after coding in C# for a good year now that I haven’t noticed the existence of such a method.
Okay, the lambda expression seems a bit messy with all the parens and all, but you get the idea. Still, its a lot better than writing a loop and having the evil “first” variable which kinda clutters the whole thing. Does anyone know an even better way of doing this?
Update: and for my answer to Eric’s next post:
string msg = "{"; IEnumerable<string> s = something; string[] arr = s.ToArray(); if (arr.Length > 1) { string[] ar2 = { string.Join(", ", arr, 0, arr.Length - 1 ), arr[arr.Length - 1] }; msg += string.Join(" and ", ar2); } else if (arr.Length == 1) msg += arr[0]; msg += "}";
Comments: 1
Use a functional language, 1 function takes list, has two cases… it’s like 3 lines of code. Something like
fun {Join L C}
case L
of H|nil then H
[] H|T then H#C#{Join T C}
end
end
{Join MyArray “, “}
Would probably do it. Also, I believe ruby/python/php have array functions which do these things; php uses implode, which is quite useful. ^^