Home >Backend Development >C#.Net Tutorial >How to merge C# List
This article is a record of knowledge points. It mainly describes how to merge List98c455a79ddfebb79781bff588e7b37e generic collections into a string string based on delimiters (such as commas). Before the earliest times, loops were often used to concatenate strings. This method not only requires writing more code, but also consumes more system resources. Nowadays, the method string.Join(string separator, string[] value) is generally used to merge the sets into strings through separators.
Here is the description of the string.Join method:
// // 摘要: // 在指定 System.String 数组的每个元素之间串联指定 // 的分隔符 System.String,从而产生单个串联的字符串。 // // 参数: // separator: // System.String。 // // value: // 一个 System.String 数组。 // // 返回结果: // System.String,包含与 separator 字符串交错的 value 的元素。 // // 异常: // System.ArgumentNullException: // value 为 null。
The following is a specific example. The example is run in the console application. Just copy the following code to the console application and run it:
static void Main(string[] args) { //字符串集合 List<string> list = new List<string>(); list.Add("a"); list.Add("b"); list.Add("c"); list.Add("d"); list.Add("e"); /* * 使用string.Join()方法 */ //使用"," 分隔符号将List<string>泛型集合合并成字符串 string strTemp1 = string.Join(",", list.ToArray()); Console.WriteLine(strTemp1); //使用 "-" 符号分隔将List<string>泛型集合合并成字符串 string strTemp2 = string.Join("-", list.ToArray()); Console.WriteLine(strTemp2); /* * 使用循环方式合成字符串 */ string strTemp3 = string.Empty; foreach (string str in list) { strTemp3 += string.Format("{0},",str); } strTemp3 = strTemp3.TrimEnd(','); Console.WriteLine(strTemp3); Console.ReadKey(); }
The output result is :
Using string.Join, you can merge List98c455a79ddfebb79781bff588e7b37e into a string through delimiters without looping.
For more C# List98c455a79ddfebb79781bff588e7b37e related articles on how to merge into strings according to separated symbols, please pay attention to the PHP Chinese website!