How to convert List & lt; string & gt; to string and list to string

Source: Internet
Author: User

How to convert List <string> to string and list to string

Convert List, string.A List can be converted to a string. This is possible with the ToArray method on the List type. We can also convert a string into a List. Conversions
The StringBuilder typeHelps with certain conversions, which are done with loops. When using StringBuilder, we must be careful with a trailing delimiter.
First example.We use the string. Join method to combine a List of strings into one string. The output can be used as a CSV record. On new. NET Framework versions, ToArray is not required.

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

List

Based on: .NET 4C# program that converts Listusing System;using System.Collections.Generic;class Program{    static void Main()    {List<string> dogs = new List<string>();dogs.Add("Aigi"); // Add string 1dogs.Add("Spitz"); // 2dogs.Add("Mastiff"); // 3dogs.Add("Finnish Spitz"); // 4dogs.Add("Briard"); // 5string dogCsv = string.Join(",", dogs.ToArray());Console.WriteLine(dogCsv);    }}OutputAigi,Spitz,Mastiff,Finnish Spitz,Briard


Example 2.Here we use the StringBuilder class to convert a List to a single string. Note that you 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 StringBuilderusing System;using System.Collections.Generic;using System.Text;class Program{    static void Main()    {List<string> cats = new List<string>(); // Create new list of stringscats.Add("Devon Rex"); // Add string 1cats.Add("Manx"); // 2cats.Add("Munchkin"); // 3cats.Add("American Curl"); // 4cats.Add("German Rex"); // 5StringBuilder builder = new StringBuilder();foreach (string cat in cats) // Loop through all strings{    builder.Append(cat).Append("|"); // Append string to StringBuilder}string result = builder.ToString(); // Get string from StringBuilderConsole.WriteLine(result);    }}OutputDevon Rex|Manx|Munchkin|American Curl|German Rex|


Example 3.Here we convert a List of ints into a single string. The StringBuilder's Append method has es 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 cocould be acquired by using a char [] and then converting to a string.

Char Array

C# program that converts List typesusing System;using System.Collections.Generic;using System.Text;class Program{    static void Main()    {List<int> safePrimes = new List<int>(); // Create list of intssafePrimes.Add(5); // Element 1safePrimes.Add(7); // Element 2safePrimes.Add(11); // Element 3safePrimes.Add(23); // Element 4StringBuilder 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);    }}Output5 7 11 23


Example 4.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 Listusing System;using System.Collections.Generic;class Program{    static void Main()    {string csv = "one,two,three"; // The input stringstring[] parts = csv.Split(','); // Call Split methodList<string> list = new List<string>(parts); // Use List constructorforeach (string item in list){    Console.WriteLine(item);}    }}Outputonetwothree


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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.