C# List<string>,List<double>,string互转。


            //【1】List转string
            List list1=new List();
            list1.Add("a");
            list1.Add("b");
            list1.Add("c");
            string s1 = string.Join(",", list1.ToArray());
            Console.WriteLine("List转string的结果s1:" + s1);

            //【2】string转List
            List list2=new List(s1.Split(','));            
            foreach (var s2 in list2)
            {
                Console.WriteLine("string转List的结果list2:" + s2);
            }

            //【3】List转List
            List strList3 = new List();
            strList3.Add("100");
            strList3.Add("200");
            strList3.Add("300");
            List doublelList3 = strList3.ConvertAll(s => Convert.ToDouble(s));
            foreach (var d3 in doublelList3)
            {
                Console.WriteLine("List转List的结果doublelList3:" + d3);
            }

            //【4】string转List
            string s4 = "100,200,300";
            List doubleLsList4 = s4.Split(',').ToList().ConvertAll(s => Convert.ToDouble(s));
            foreach (var d4 in doubleLsList4)
            {
                Console.WriteLine("List转List的结果doubleLsList4:" + d4);
            }

            //【5】List转string
            string s5 = string.Join(",", doubleLsList4.ConvertAll(s => Convert.ToString(s)));
            Console.WriteLine("List转string的结果s5:" + s5);

结果:

List转string的结果s1:a,b,c
string转List的结果list2:a
string转List的结果list2:b
string转List的结果list2:c
List转List的结果doublelList3:100
List转List的结果doublelList3:200
List转List的结果doublelList3:300
List转List的结果doubleLsList4:100
List转List的结果doubleLsList4:200
List转List的结果doubleLsList4:300
List转string的结果s5:100,200,300

相关