引用自 CSDN某帖子 考考你的编程艺术能力,谁能将这段代码改的 【雅】 一点~
本人感觉70楼 avon520 解法很不错
protected void Page_Load(object sender, EventArgs e)
{
List<string> lst = new List<string> { "Apple", "MS", "Google" };
string s = joinList(lst, ";");
}
private string joinList(List<string> lst, string separator)
{
if (lst == null)
throw new Exception("can not be null");
if (lst.Count == 0)
return "";
return string.Join(separator, lst.ToArray());
}
另附 以下解法:
public string JoinList1(string[] StringList)
{
StringBuilder sb = new StringBuilder();
foreach (string StrArg in StringList)
{
sb.Append(StrArg);
sb.Append(";");
}
return sb.ToString().TrimEnd(';');
}
public string JoinList2(List<string> StringList)
{
StringBuilder lzsb = new StringBuilder();
foreach (string StrArg in StringList)
{
lzsb.Append(StrArg);
lzsb.Append(";");
}
return lzsb.ToString().TrimEnd(';');
}
在这里引用:
string[] strlist = new string[] { "Apple", "MS", "Google"};
string str1 = JoinList1(strlist);
Response.Write(str1);
Response.Write("\r\n");
List<string> lst = new List<string> { "Apple", "MS", "Google" };
string str2 = JoinList2(lst);
Response.Write(str2);