if i have:
List<Car>
where car is:
public class Car
{
public int Year;
public string Name;
}
and i want to take this array and create a concatenated string by ","
so it would return:
"Toyota, Ford, Chevy"
i can do it manually like this:
private static string CreateConcatenatedList(List<Car> parts_)
{
StringBuilder b = new StringBuilder();
foreach (Car bp in parts_)
{
b.Append(bp.Name + ", ");
}
b.Remove(b.Length - 2, 2);
return b.ToString();
}
but i thought there might be a more elegant way
The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
To convert a JavaScript array into a string, you can use the built-in Array method called toString . Keep in mind that the toString method can't be used on an array of objects because it will return [object Object] instead of the actual values.
toString() method: Arrays. toString() method is used to return a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters “, ” (a comma followed by a space).
You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.
List<Car> cars = //whatever;
string concat = String.Join(",", cars.Select(c => c.Name).ToArray());
EDIT: you could also use Aggregate if you're worried about creating the intermediate array:
string concat = cars.Select(c => c.Name).Aggregate(new StringBuilder(), (sb, current) =>
{
return sb.Length == 0 ? sb.Append(current) : sb.AppendFormat(",{0}", current);
}).ToString();
Because you asked in a comment to Lees answer whether it is faster/slower or just less code. I tried a bit, and wrote a small car class:
public class Car
{
public string Name { get; set; }
public Car(string name) { Name = name; }
}
Tested it with randomly generated strings of length 5-10:
private static Random random = new Random((int)DateTime.Now.Ticks);
private static string RandomString(int min, int max)
{
string str = "";
int size = random.Next(min, max + 1);
for (int i = 0; i < size; i++)
str += Convert.ToChar(Convert.ToInt32(
Math.Floor(26 * random.NextDouble() + 65)));
return str;
}
public static void MeassureTicks(int numberCars, int minLength, int maxLength)
{
// Generate random list
List<Car> cars = Enumerable.Range(0, numberCars)
.Select(x => new Car(RandomString(
minLength, maxLength))).ToList();
Stopwatch sw1 = new Stopwatch(), sw2 = new Stopwatch(),
sw3 = new Stopwatch(), sw4 = new Stopwatch();
sw1.Start();
string concat1 = CreateConcatenatedList(cars);
sw1.Stop();
sw2.Start();
string concat2 = String.Join(",", cars.Select(c => c.Name).ToArray());
sw2.Stop();
sw3.Start();
if (numberCars <= 5000)
{
string concat3 = cars.Select(c => c.Name).Aggregate("",
(str, current) =>
{
return str.Length == 0 ? str = current :
str += "," + current;
}).ToString();
}
sw3.Stop();
sw4.Start();
string concat4 = cars.Select(c => c.Name).Aggregate(
new StringBuilder(), (sb, current) =>
{
return sb.Length == 0 ? sb.Append(current) :
sb.AppendFormat(",{0}", current);
}).ToString();
sw4.Stop();
Console.WriteLine(string.Format("{0} car strings joined:\n" +
"\tYour method: {1} ticks\n" +
"\tLinq+String.Join: {2} ticks\n" +
"\tLinq+Aggregate+String.Concat: {3} ticks\n" +
"\tLinq+Aggregate+StringBuilder: {4} ticks\n",
cars.Count, sw1.ElapsedTicks, sw2.ElapsedTicks,
numberCars <= 5000 ? sw3.ElapsedTicks.ToString() : "-",
sw4.ElapsedTicks));
Update: I am now trying both methods that are using aggregate, too.
The outputs are on my pc for some different number of cars:
5 car strings joined:
Your method: 14 ticks
Linq+String.Join: 20 ticks
Linq+Aggregate+String.Concat: 11 ticks
Linq+Aggregate+StringBuilder: 15 ticks
50 car strings joined:
Your method: 50 ticks
Linq+String.Join: 45 ticks
Linq+Aggregate+String.Concat: 70 ticks
Linq+Aggregate+StringBuilder: 73 ticks
500 car strings joined:
Your method: 355 ticks
Linq+String.Join: 348 ticks
Linq+Aggregate+String.Concat: 5365 ticks
Linq+Aggregate+StringBuilder: 619 ticks
5000 car strings joined:
Your method: 3584 ticks
Linq+String.Join: 3357 ticks
Linq+Aggregate+String.Concat: 379635 ticks
Linq+Aggregate+StringBuilder: 6078 ticks
50000 car strings joined:
Your method: 33705 ticks
Linq+String.Join: 34082 ticks
Linq+Aggregate+String.Concat: - ticks
Linq+Aggregate+StringBuilder: 92839 ticks
500000 car strings joined:
Your method: 508439 ticks
Linq+String.Join: 376339 ticks
Linq+Aggregate+String.Concat: - ticks
Linq+Aggregate+StringBuilder: 616048 ticks
The Linq+String.Join
method is indeed a bit faster and less code. Aggregate together with the StringBuilter
scales very well (not like the string concatenation), but is a bit slower. So either use your method, or Linq+String.Join
, which is a nice oneliner and also easily readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With