我正在寻找一个与php sprintf函数等效的C#。
我有下面的字符串:
"There are %s hits, %s are an exact match."
我希望将%s
替换为查询返回的数字。在php中,我将执行以下操作:
$res = sprintf("There are %s hits, %s are an exact match", $number1, $number2);
我如何在C#中做到这一点?我考虑过string.replace()
,但它只适用于应该替换的一部分。在这种情况下,有多个。
发布于 2011-03-16 17:49:00
您正在寻找String.Format
String myStr = String.Format("There are {0} hits, {1} are an exact match",
number1, number2);
格式说明符与PHP中的稍有不同。Here is an article explaining format specifiers。
发布于 2011-03-16 17:49:23
您可以使用String.Format
。
string s = String.Format("There are {0} hits, {1} are an exact match", number1, number2);
https://stackoverflow.com/questions/5329470
复制