I tried to append the items in a List<string>
to a StringBuilder
with LINQ:
items.Select(i => sb.Append(i + ","));
I found a similar question here which explains why the above doesn't work, but I couldn't find an Each
of ForEach
or anything similar on List
which I could use instead.
Is there a neat way of doing this in a one-liner?
StringBuilder. append(boolean a) is an inbuilt method in Java which is used to append the string representation of the boolean argument to a given sequence. Syntax : public StringBuilder append(boolean a) Parameter: This method accepts a single parameter a of boolean type and refers to the Boolean value to be appended.
append(int i) method appends the string representation of the int argument to this sequence.
append(String str) method appends the specified string to this character sequence. The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument.
Add/Append String to StringBuilderUse the Append() method to append a string at the end of the current StringBuilder object. If a StringBuilder does not contain any string yet, it will add it. The AppendLine() method append a string with the newline character at the end.
items.ForEach(item => sb.Append(item + ","));
You could use a simple foreach
loop. That way you have statements which modify the StringBuilder, instead of using an expression with side-effects.
And perhaps your problem is better solved with String.Join(",", items)
.
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