Convert int List to string List c#

Here’s how to convert a List to List. Use the List[T].ConvertAll method

C#

// Convert a List to List
List lstNum = new List[new int[] { 3, 6, 7, 9 }];
lstNew = lstNum.ConvertAll[delegate[int i]{

return i.ToString[];

}];

PrintList[lstNew];


VB.NET

' Convert a List to List
Dim lstNum As New List[Of Integer][New Integer[] { 3, 6, 7, 9 }]
lstNew = lstNum.ConvertAll[Of String][Function[i As Integer] i.ToString[]]
PrintList[lstNew]

OUTPUT

Read more tips over here Some Common Operations using List

Page 2

C# Convert List to StringUse the string.Join method and StringBuilder to convert Lists and strings.

Convert List, string. Think of a sentence. It contains some words. We could represent a sentence as a single string—one with spaces. But a list of strings [of words] is sometimes better.

We can convert our string into a List of smaller strings. For the reverse, we can convert our List into a string. A List can be joined directly as it is an IEnumerable type.

IEnumerable

First example. Often the best way to convert a List of strings into an array is the string.Join method. This is built into the .NET Framework, so we do not need to write any custom code.

Part 1 We create a List of strings by invoking the List constructor, and then calling Add[] 3 times.

Part 2 We use the string.Join method to combine a List of strings into one string. The output can be used as a CSV record.

Join

Info In previous versions of .NET, we had to call ToArray on a List before using Join. In older programs this is still required.

C# program that converts List to string

using System; using System.Collections.Generic; class Program { static void Main[] { // Part 1: create list of strings. List animals = new List[]; animals.Add["bird"]; animals.Add["bee"]; animals.Add["cat"]; // Part 2: use string.Join and pass the list as an argument. string result = string.Join[",", animals]; Console.WriteLine[$"RESULT: {result}"]; } }RESULT: bird,bee,cat

StringBuilder. Here we use the StringBuilder class to convert a List to a single string. We can convert a List of any object type into a string this way.

StringBuilder

Final delimiter The example has a final delimiter on the end. This is not present in code that uses string.Join—it can be inconvenient.

TrimEnd Sometimes, it is good to remove the end delimiter with TrimEnd. Other times it is best left alone.

TrimEnd, TrimStart

C# program that uses List and StringBuilder

using System; using System.Collections.Generic; using System.Text; class Program { static void Main[] { List cats = new List[]; cats.Add["Devon Rex"]; cats.Add["Manx"]; cats.Add["Munchkin"]; cats.Add["American Curl"]; cats.Add["German Rex"]; StringBuilder builder = new StringBuilder[]; // Loop through all strings. foreach [string cat in cats] { // Append string to StringBuilder. builder.Append[cat].Append["|"]; } // Get string from StringBuilder. string result = builder.ToString[]; Console.WriteLine[result]; } }Devon Rex|Manx|Munchkin|American Curl|German Rex|

Int List. Here we convert a List of ints into a single string. The StringBuilder's Append method receives a variety of types. We can simply pass it the int.

And Append[] will handle the int on its own. It will convert it to a string and append it.

Performance StringBuilder is fast for most programs. More speed could be acquired by using a char[] and then converting to a string.

Char Array

C# program that converts List types

using System; using System.Collections.Generic; using System.Text; class Program { static void Main[] { List safePrimes = new List[]; safePrimes.Add[5]; safePrimes.Add[7]; safePrimes.Add[11]; safePrimes.Add[23]; StringBuilder builder = new StringBuilder[]; foreach [int safePrime in safePrimes] { // Append each int to the StringBuilder overload. builder.Append[safePrime].Append[" "]; } string result = builder.ToString[]; Console.WriteLine[result]; } }5 7 11 23

CSV string. Finally, we get a List of strings from a string in CSV format. This requires the Split method. If you require per-item conversion, loop over the string array returned by Split.

C# program that converts string to List

using System; using System.Collections.Generic; class Program { static void Main[] { string csv = "one,two,three"; string[] parts = csv.Split[',']; // Use List constructor. List list = new List[parts]; foreach [string item in list] { Console.WriteLine[item]; } } }one two three

A summary. We converted Lists and strings using the string.Join methods and the StringBuilder approach. The List is easily concatenated and stored in a database or file with these methods.

List

© 2007-2022 sam allen.

see site info on the changelog.

This post will discuss how to convert a list to a string in C# where individual elements of the list are joined into a single string using a given delimiter. The delimiter should not be added before or after the list.

1. Using String.Join[] method

The recommended solution is to use the String.Join[] method of the string class, which joins elements of the specified array or collection together with the specified delimiter.

using System.Collections.Generic;

    public static void Main[]

        List list = new List[] { "A", "B", "C" };

        string str = String.Join[delim, list];

Download  Run Code

2. Using Enumerable.Aggregate[] method

Like the above method, LINQ provides the Aggregate[] method, which can join elements of a list using a delimiter. The following code example shows how to implement this:

using System.Collections.Generic;

    public static void Main[]

        List list = new List[] { "A", "B", "C" };

        string str = list.Aggregate[[x, y] => x + delim + y];

Download  Run Code

3. Using StringBuilder

A naive solution is to loop through the list and concatenate each list element to the StringBuilder instance with the specified delimiter. Finally, we return the string representation of the StringBuilder. Note that the solution handles trailing delimiters’ characters.

using System.Collections.Generic;

    public static void Main[]

        List list = new List[] { "A", "B", "C" };

        StringBuilder sb = new StringBuilder[];

        for [int i = 0; i

Chủ Đề