在一个字符串代替多个占位符 [英] Replacing multiple placeholders in a string
本文介绍了在一个字符串代替多个占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2占位符的字符串,我想基于价值的计算上运行时更换。什么是做到这一点的最好办法。我用下面的code单占位符代替,
字符串posttData = @数据= google.visualization.arrayToDataTable([
['标签','值'],
['结果',{0}]
]);
VAR的选择= {
宽度:1200,高度:500,redFrom:90,redTo:100,yellowFrom:75,yellowTo:90,器minorTicks:5
};
....结果计算
VAR X = posttData.Replace({0},result.ToString());
ScriptManager.RegisterStartupScript(此,this.GetType(),测试中,x,真);
我如何做同样多的替代品?如果我需要添加另一种选择,
VAR选项= {
宽度:1200,高度:500,redFrom:90,redTo:100,yellowFrom:75,yellowTo:90,器minorTicks:5,最大值:{1}
解决方案
怎么样的 的String.Format
?
字符串str =的String.format(@有些值{0}和一些价值{1},1,2);
应该产生:
有些值1和一些价值2
块引用>您可以替换
1
和2
与任何结果,您可能需要。I have a string with 2 placeholders, that I want to replace on runtime based on value calculations. what is the best way to do this. I have used the code below for single placeholder replacement,
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 };
....result calculation
var x = posttData.Replace("{0}", result.ToString()); ScriptManager.RegisterStartupScript(this, this.GetType(), "test", x, true);
How do I do the same for multiple replacements? if I need to add another option,
var options = { width: 1200, height: 500, redFrom: 90, redTo: 100, yellowFrom: 75, yellowTo: 90, minorTicks: 5, max: {1}
解决方案How about the
String.Format
?String str = String.format(@"Some value {0} and some value {1}", 1, 2);
Should yield:
Some value 1 and some value 2
You can replace
1
and2
with whatever results you might need.这篇关于在一个字符串代替多个占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文