Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a list of strings to a single string

Tags:

string

c#

list

List<string> MyList = (List<string>)Session["MyList"]; 

MyList contains values like: 12 34 55 23.

I tried using the code below, however the values disappear.

string Something = Convert.ToString(MyList); 

I also need each value to be separated with a comma (",").

How can I convert List<string> Mylist to string?

like image 410
user2869820 Avatar asked Oct 13 '13 13:10

user2869820


People also ask

How do you convert a list of strings to one 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.

How do I merge a list of strings into a single string in Python?

You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

Can we convert list to string in Java?

We use the toString() method of the list to convert the list into a string.

How do I join a list of elements into a string?

join() is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string.


2 Answers

string Something = string.Join(",", MyList); 
like image 101
ProgramFOX Avatar answered Sep 23 '22 01:09

ProgramFOX


Try this code:

var list = new List<string> {"12", "13", "14"}; var result = string.Join(",", list); Console.WriteLine(result); 

The result is: "12,13,14"

like image 29
MUG4N Avatar answered Sep 26 '22 01:09

MUG4N