I have an array of object e.g:
MyObject[] objs;
and within MyObject it contains a string property,
object[0].stringValue
If I want to join the whole array of objects by their stringValue
, how can I do it?
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.
In C#, Join() is a string method. This method is used to concatenates the members of a collection or the elements of the specified array, using the specified separator between each member or element. This method can be overloaded by passing different parameters to it.
The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.
Using StringBufferCreate an empty String Buffer object. Traverse through the elements of the String array using loop. In the loop, append each element of the array to the StringBuffer object using the append() method. Finally convert the StringBuffer object to string using the toString() method.
string.Join(",",objs.Select(w=>w.stringValue))
Do you mean to use string.Join
to concatenate a property from multiple MyObject objects into one single string?
Then:
string str = string.Join(",", objs.Select(x => x.SomeProperty));
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