将字符串添加到以逗号分隔的字符串

Add strings to a string separated with a comma

我正在尝试将字符串添加到字符串中,但用逗号分隔。最后我想删除 , 和 space。什么是最干净的方法?

var message =  $"Error message : ";

if (Parameters != null)
{
    Parameters
        .ToList()
        .ForEach(x => message += $"{x.Key} - {x.Value}, "); // <- remove the , " at the end
}

return message;

参数是一个 Dictionary

将此与 String.Join

一起使用
message += string.Join(",",Parameters.ToList().Select(x => $"{x.Key} - {x.Value}"));

你可以使用join方法但是如果你想知道idea你可以看这段代码

var message =  $"Error message : ";
var sep = "";
if (Parameters != null)
{
    Parameters
        .ToList()
        .ForEach(x => {
            message += $"{sep} {x.Key} - {x.Value}";
            sep = ",";
         });
}

return message;