从列表创建字符串的另一种方法
本文关键字:另一种 方法 字符串 列表 创建 | 更新日期: 2023-09-27 18:15:13
有这样的函数,它从id列表中创建字符串我尝试在50个线程中运行它,30秒后它挂起app
public string genText(List<string> list)
{
string text = @"{""text"":""";
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text += "!" + " ";
else
text += "!" + @""", ";
}
text += @"""start"":[";
for (int i = 0, lim = -2; i < list.Count; i++)
{
lim += 2;
if (i < list.Count - 1)
text += @"""" + lim + @""",";
else
text += @"""" + lim + @"""], ";
}
text += @"""end"":[";
for (int i = 1, lim = -1; i < list.Count + 1; i++)
{
lim += 2;
if (i < list.Count)
text += @"""" + lim + @""",";
else
text += @"""" + lim + @"""], ";
}
text += @"""type"":[";
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text += @"""USER"",";
else
text += @"""USER""], ";
}
text += @"""objectId"":[";
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text += @"""" + list[i] + @""",";
else
text += @"""" + list[i] + @"""]}";
}
return text;
}
我什么 年代最好的(替代品)的写作方式等功能 n ?
预期结果:{"text":"! ! ! ! ! ! ! ! ! ! ! ! ! ! !", "start":["0","2","4","6","8","10","12","14","16","18","20","22","24","26","28"], "end":["1","3","5","7","9","11","13","15","17","19","21","23","25","27","29"], "type":["USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER"], "objectId":["524231066205","363249609235","509321690322","551832845250","273337925148","553378679618","552270073142","256641407","545454406232","548096729194","555315805314","553271555117","573655339037","518779453704","486606264340"]}
比起串接字符串,使用StringBuilder类更有效。
public string genText(List<string> list)
{
StringBuilder text = new StringBuilder();
text.Append(@"{""text"":""");
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text.Append("!" + " ");
...
return text.ToString();
}
但是正如其他人在评论中提到的,如果你想创建Json数据,请使用Json库