How C # list<string> is combined into strings based on delimiters

Source: Internet
Author: User
This article, as a knowledge point record, mainly describes how to combine a list<string> generic collection into a string string based on delimiters (such as commas). Before the earliest, it was often used in a circular way to stitch into strings, not only to write more code, but also to consume more system resources. And now generally use string. Join (String separator, string[] value) This method to combine the collection through delimiters into strings.

Here is the string. Description of the Join method:

Abstract:     A single concatenated string is produced by concatenating the specified///delimiter System.String between each element of the specified System.String array. Parameters://   separator://     System.String.   value://     a System.String array. Returns the result://     System.String, containing the element of value interleaved with the separator string. Exception://   system.argumentnullexception://     value is null.

The following is a concrete example of running a console application that copies the following code to a console application:

static void Main (string[] args) {//String collection list<string> List = new list<string> (); List.            ADD ("a"); List.            ADD ("B"); List.            ADD ("C"); List.            ADD ("D"); List.            ADD ("E"); /* Use String. The Join () method///Use the "," delimiter to combine the list<string> generic collection into a string strTemp1 = string. Join (",", list.            ToArray ());            Console.WriteLine (STRTEMP1); Use the "-" symbol to separate the list<string> generic collection into strings string strTemp2 = String. Join ("-", list.            ToArray ());            Console.WriteLine (STRTEMP2); /* * Use a looping method to synthesize the string */String StrTemp3 = String.            Empty; foreach (string str in list) {StrTemp3 + = string.            Format ("{0},", str);            } StrTemp3 = Strtemp3.trimend (', ');            Console.WriteLine (StrTemp3);                   Console.readkey (); }

The result of the output is:

Use String. Joins can be used to combine list<string> into a single string without looping through delimiters.



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.