标签:
将数值数组拼合为字符串,一直使用循环的方式,代码虽简单,但总想更简洁,重构嘛
int[] intArray = new int[] { 1, 2, 3, 4, 5 }; string result = string.Empty; for (int i = 0; i < intArray.Length; i++) { if (!string.IsNullOrEmpty(result)) result += "," + intArray[i]; else result = intArray[i].ToString(); }
今天才知道真的有小技巧,简洁的可爱!
int数组转有分隔符的字符串
int[] ints = { 1, 2, 3 }; string[] strs = Array.ConvertAll<int, string>(ints, delegate(int input) { return input.ToString();}); string result = string.Join(";", strs);
int数组转string[]数组也不需要写循环
int[] ints={1,2,3,4}; List list = Arrays.asList(ints); String[] arr = (String[])list.toArray(new String[ints.length]);
int[] ints = { 1, 2, 3 }; string[] output = Array.ConvertAll<int, string>( ints, i => i.ToString() );
标签:
原文地址:http://www.cnblogs.com/GISRSMAN/p/5728595.html