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.