string[] bits = list.Split(','); // Param arrays are your friend for (int i=0; i < bits.Length; i++) { bits[i] = "'" + bits[i] + "'"; } return string.Join(",", bits);
Or you can use LINQ, especially with the version of String.Join that supports IEnumerable<string> ...
return list.Split(',').Select(x => "'" + x + "'").JoinStrings(",");
There is an implementation of JoinStrings elsewhere in SO ... I will look for it.
EDIT: Well, not the way I thought about concatenated strings, so here it is:
public static string JoinStrings<T>(this IEnumerable<T> source, string separator) { StringBuilder builder = new StringBuilder(); bool first = true; foreach (T element in source) { if (first) { first = false; } else { builder.Append(separator); } builder.Append(element); } return builder.ToString(); }
These days, string.Join has a common overload instead, so you can just use:
return string.Join(",", list.Split(',').Select(x => $"'{x}'"));
Jon Skeet Oct 31 '08 at 16:01 2008-10-31 16:01
source share