大家经常需要数组加逗号拼接成字符串的情况传统作法就是写for,foreach拼接, 现给出优化改良版数组,List,等集合需要加逗号或其它符合转成字符串方法:
1 List<string> ls=new List<string>{ "lshiyh1", "lshiyh2", "lshiyh3" }; 2 string[] strArr = { "shiyh1", "shiyh2", "shiyh3" }; 3 //string[] strArr1 = ls.ToArray(); 4 5 string strString = string.Join(",", strArr); 6 string lStrString = string.Join(",", ls); 7 Console.WriteLine("数组转字符串结果{0},List转字符串结果{1}", strString,lStrString); 8 strString = string.Join(",", strArr.Select(p => string.Format("[{0}]", p))); 9 lStrString = string.Join(",", ls.Select(p => string.Format("[{0}]", p))); 10 Console.WriteLine("数组转字符串格式化结果{0},List转字符串格式化结果{1}", strString, lStrString);
输出结果:
本文为博主原创文章,未经博主允许不得转载。
NET技术QQ群号:456257217有问题的可以在群里面提问