今天在项目中,要将字符串数组转成字符串,随手就写了个方法
1 /// <summary> 2 /// 将字符串数组转换成字符串 3 /// </summary> 4 /// <param name="strArray">需要转换的字符串</param> 5 /// <returns>合并完成的字符串</returns> 6 public string arrayToString(string[] strArray) 7 { 8 StringBuilder str = new StringBuilder(); 9 for (int i=0; i < strArray.Length; i++) 10 { 11 12 if (i > 0) 13 { 14 //分割符可根据需要自行修改 15 str.Append(","); 16 } 17 str.Append(strArray[i]); 18 } 19 return str.ToString(); 20 }
使用
string[] str = { "ss", "22as2fas22", "ffsaa", "asdf", "asdd", "77as7" };
string str2 = arrayToString(str);
Console.WriteLine("输出转换后的字符串:"); Console.WriteLine(str2); Console.WriteLine(); //将字符串转换为数组 string[] str3 = str2.Split(','); Console.WriteLine("输出数组元素:"); //输出数组元素 foreach (string i in str3) { Console.WriteLine(i); }
Console.WriteLine(); Console.Read();
源码地址:
http://ishare.iask.sina.com.cn/f/17890345.html
|
请发表评论