数组,集合分割函数,join()

join函数定义如下:

     //     串联类型为 System.String 的 System.Collections.Generic.IEnumerable<T> 构造集合的成员,其中在每个成员之间使用指定的分隔符。    
     public static string Join(string separator, IEnumerable<string> values);
        //     串联字符串集合的成员,其中在每个成员之间使用指定的分隔符。
        public static string Join<T>(string separator, IEnumerable<T> values);
        //     串联对象数组的各个元素,其中在每个元素之间使用指定的分隔符。
        public static string Join(string separator, params object[] values);
        //     串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符。
        public static string Join(string separator, params string[] value);
        //     串联字符串数组的指定元素,其中在每个元素之间使用指定的分隔符。
        public static string Join(string separator, string[] value, int startIndex, int count);

比如,我们常用“ "来把一个数组给串联起来。

这里展示一个实例,分别把一个数组和一个List集合给串联起来。

 static void Main(string[] args)
        {
            string[] param = new string[] { "小红", "小明", "小张", "小刘" };
            string param1 = string.Join(",", param);
            Console.WriteLine(param1);

            List<string> list = new List<string>();
            list.Add("列表1");
            list.Add("列表2");
            list.Add("列表3");
            list.Add("列表4");
            string param2 = string.Join(",", list);
            Console.WriteLine(param2);

            Console.ReadLine();
        }

结果如下:

原文地址:https://www.cnblogs.com/alsf/p/6255578.html