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
?
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.
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.
We use the toString() method of the list to convert the list 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.
string Something = string.Join(",", MyList);
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"
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