Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use string.join to join value from an object array?

Tags:

string

c#

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?

like image 897
jojo Avatar asked Mar 17 '11 22:03

jojo


People also ask

How would you Join array items into a string?

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.

How does string Join work in C#?

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.

What is string Join method?

The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.

How do you Join an array of strings into a single string in Java?

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.


2 Answers

string.Join(",",objs.Select(w=>w.stringValue))
like image 185
Blindy Avatar answered Oct 13 '22 18:10

Blindy


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));
like image 31
Adam Price Avatar answered Oct 13 '22 20:10

Adam Price