How to convert list<string> to string

Source: Internet
Author: User

Convert List, String. A List can is converted to a string. This was possible with the ToArray method on the List type. We can also convert a string into a list.conversions
The StringBuilder type helps with certain conversions, which is done with loops. When using StringBuilder, we must is 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 the 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 the is still required.

List

Based on: . NET 4C # program, converts Listusing system;using system.collections.generic;class program{    static void Main ()    {list<string> dogs = new list<string> ();d ogs. 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);    }} Output Aigi,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 to a string this to. StringBuilder

Final delimiter: The example have a final delimiter on the end. This isn't present in code, uses string. Join. It can be inconvenient.

TrimEnd: Sometimes, it is good to remove the end delimiter with TrimEnd. Other times it's 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 Strin Gscats. 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);}    } Output Devon rex| Manx| munchkin| American curl| German rex|


Example 3. Here we convert a List of INTs to a single string. The StringBuilder ' s Append method receives a variety of types. We can simply pass it the Int.

and : Append () would handle the int on its own. It would convert it to a string and append it.

Performance: StringBuilder is fast for most programs. More speed could is 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 per int to T He 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 This 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 the 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.

How to convert list<string> to string

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.