C# 替换字符串中的多个占位符

C# 替换字符串中的多个占位符,c#,asp.net,C#,Asp.net,我有一个带有2个占位符的字符串,我想在运行时基于值计算替换它。最好的方法是什么。我使用下面的代码替换单个占位符 String posttData = @"data = google.visualization.arrayToDataTable([ ['Label', 'Value'], ['Result', {0}]

我有一个带有2个占位符的字符串,我想在运行时基于值计算替换它。最好的方法是什么。我使用下面的代码替换单个占位符

   String posttData = @"data = google.visualization.arrayToDataTable([
                                ['Label', 'Value'],
                                ['Result', {0}]                           
                                ]);
    var options = {
        width: 1200, height: 500, redFrom: 90, redTo: 100, yellowFrom: 75, yellowTo: 90, minorTicks: 5
    };
…结果计算

var x = posttData.Replace("{0}", result.ToString());
ScriptManager.RegisterStartupScript(this, this.GetType(), "test", x, true);
如何对多个替换执行相同的操作?如果我需要添加另一个选项

var options = {
                   width: 1200, height: 500, redFrom: 90, redTo: 100, yellowFrom: 75, yellowTo: 90, minorTicks: 5, max: {1}
这辆车怎么样

应产生:

有些值为1,有些值为2


您可以用可能需要的任何结果替换
1
2

ia;方法创建一个扩展方法以便于使用

 public static class StringExtensions
    {  
        public static string FormatWith(this string source, params object[] args)
        {
            return string.Format(source, args);
        }
}
打电话

"Some value {0} and some value {1}".FormatWith(1,2);

我建议使用字符串插值:

$"Some value {1} and some value {2}"
其中“1”和“2”可以是您需要的任何结果/表达式

"Some value {0} and some value {1}".FormatWith(1,2);
$"Some value {1} and some value {2}"