0

我正在寻找 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()但这只适用于应该更换的 1 件。在这种情况下,有多个。

4

2 回答 2

9

您正在寻找String.Format

String myStr = String.Format("There are {0} hits, {1} are an exact match",
                              number1, number2);

格式说明符与 PHP 中的有点不同。这是一篇解释格式说明符的文章

于 2011-03-16T17:48:59.897 回答
3

你用String.Format.

string s = String.Format("There are {0} hits, {1} are an exact match", number1, number2);
于 2011-03-16T17:49:22.710 回答