Как перевести из list в string c

от admin

C# List to string

C# List to string tutorial shows how to convert a List to a string in C#.

To turn a list of elements into a single string in C#, we will utilize the string.Join method, StringBuilder object, Enumerable.Aggregate method and the string concatenation operator.

The string.Join metho concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. The StringBuilder class is used to dynamically build strings. The Enumerable.Aggregate method applies an accumulator function over a sequence of values.

C# uses the + operator to concatenate strings.

Advertisements C# List to string example

In the following example, we transform a list to a string with the string.Join method.

In the example, we create a slug from a list of words.

C# List to string example II

In the second example, we use the StringBuilder class.

We go through the list in a foreach loop and dynamically build the string with the StringBuilder’s Append method. The ToString method converts the StringBuilder object to a string.

Advertisements C# List to string example III

The next example uses the Enumerable.Aggregate method.

The example uses the string interpolation operation in the accumulator function.

C# List to string example IV

Finally, we build the string with the string concatenation operation.

We go through all the elements of the list with the ForEach method. We build the string using the string concatenation operator. (In our case the += compound operator.)

Как перевести из list в string c

What is FinalOutput?

Is this a List<Object> which has an int, decimal or whatever per item.

Is FinalOutput a complex type?

If it’s just a List<Object> you have there you could do.

If FinalOutput is not a single object then you would have to ToString each property and append somehow in the select instead.

Hope that helps.

  • Edited by Andy ONeill Tuesday, January 12, 2016 4:00 PM
  • Marked as answer by _Joan B_ Tuesday, January 12, 2016 4:23 PM

All replies

Are you looking for something like this?

Hope it helps. Spiri

  • Edited by Spiri91 Tuesday, January 12, 2016 3:38 PM

You could create a ToString() method in your FinalOutput class:

Then your loop would look like this:

Hope it helps. Spiri

Thanks for your responses! I decided to define the elements of my list as strings rather than decimals and integers. I convert ToString as I’m loading them into the list<t>.

Probably a more efficient way to do this but I’m strapped for time and I can type faster than it’ll take me to figure out a ‘shorter’ way to do this.

What is FinalOutput?

Is this a List<Object> which has an int, decimal or whatever per item.

Is FinalOutput a complex type?

If it’s just a List<Object> you have there you could do.

If FinalOutput is not a single object then you would have to ToString each property and append somehow in the select instead.

Читать:
Как настроить переадресацию на телефоне cisco 7911

Hope that helps.

  • Edited by Andy ONeill Tuesday, January 12, 2016 4:00 PM
  • Marked as answer by _Joan B_ Tuesday, January 12, 2016 4:23 PM

I’m not sure how to answer your question because I don’t know what it’s called (my apologies, I’m not very good at ‘technical terms’).

Basically I have a custom class set up and that’s what I’m using to create the list from. Here’s an abbreviated example of my custom class that shows 3 items (the actual class in my program has over 100 items).

How to convert list to string with delimiter C#

In this post, we will learn about how to convert a list to a string with delimiter C#. We are using the LINQ aggregate() function,StringBuilder append() and Array.ConvertAll() to convert list to string with delimiter with examples.

How to convert list to string with delimiter

In this C# example, we are using Linq Aggregate() function to convert a list to a string with a delimiter. We have used a comma(,) delimiter,instead of comma(,) you can use semicolon(;),hyphen(/),underscore (_) as per need.

Convert List<string> to String in C#

In this tutorial, we will discuss methods to convert a List<string> to a string variable in C#.

Convert List<string> to String With the Linq Method in C#

The Linq or language integrated query can perform robust text manipulation in C#. The Linq has an Aggregate() function that can convert a list of strings to a string variable. The following code example shows us how to convert a List<string> to a string with the Linq method in C#.

We create the list of strings names and insert the values < "Ross", "Joey", "Chandler" >in the names . Then we join the strings inside the names list with the , as the separator between them using the Aggregate() function in C#.

This method is very slow and is not recommended. It is the same as running a foreach loop and concatenating each element.

Convert List<string> to String With the String.Join() Function in C#

The String.Join(separator, Strings) function can concatenate the Strings with the specified separator in C#. The String.Join() function returns a string formed by joining the Strings parameter with the specified separator .

The following code example shows us how we can convert a List<string> to a string with the String.Join() function in C#.

We create the list of strings names and insert the values < "Ross", "Joey", "Chandler" >in the names . Then we join the strings inside the names list with the , as the separator between them using the String.Join() function in C#.

This method is much faster and is preferable to the previous method.

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

Похожие статьи