C #: how to convert List & lt; string & gt; to a string connected with a separator ?,

Source: Internet
Author: User

C #: how to convert List <string> to a string connected with a separator ?,

I often use a knowledge point. I went to Baidu every time I used it, and today I used it again (I ran to ferry again). To avoid forgetting it when I used it, I searched it and recorded it today.

How to combine the List <string> generic set into a string Based on the delimiter (such as a comma? In the past, we used loops to concatenate strings. Not only do we need to write more code, but it also consumes more system resources. GenerallyString. Join (string separator, IEnumerable <T> values)This method is used to combine a set into a string using separators.

Here is the complete description of the string. Join method:

//
// Summary:
// System. Collections. Generic. IEnumerable <T> of the series type System. String is used to construct the members of the set. The specified separator is used between each member.
//
// Parameters:
// Separator:
// The string to be used as the separator.
//
// Values:
// A set of strings to be concatenated.
//
// Return result:
// A string composed of values members, which are separated by the separator string.
//
// Exception:
// System. ArgumentNullException:
// Values is null.
[ComVisible (false)]
Public static string Join (string separator, IEnumerable <string> values );

The following code example:

List <string> testList = new List <string> {"Jim", "John", "Linda", "Sam "};
String result = string. Join (",", testList );
Console. WriteLine ("These People are your partner:" + result );

Output result:

 

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.