我正在嘗試將字串添加到字串中,但用逗號分隔。最后我想洗掉 , 和空間。最干凈的方法是什么?
var message = $"Error message : ";
if (Parameters != null)
{
Parameters
.ToList()
.ForEach(x => message = $"{x.Key} - {x.Value}, "); // <- remove the , " at the end
}
return message;
引數是一個字典<string, string>
uj5u.com熱心網友回復:
使用這個String.Join
message = string.Join(",",Parameters.ToList().Select(x => $"{x.Key} - {x.Value}"));
uj5u.com熱心網友回復:
您可以使用 join 方法,但如果您想知道想法,可以查看此代碼
var message = $"Error message : ";
var sep = "";
if (Parameters != null)
{
Parameters
.ToList()
.ForEach(x => {
message = $"{sep} {x.Key} - {x.Value}";
sep = ",";
});
}
return message;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/470753.html
上一篇:如何用子字串制作唯一的字串?
下一篇:如何在R中的嵌套括號內反轉字符?
