开发者

C#: php sprintf equivalent

I'm looking for a C# equivalent for the php sprintf function.

I have the folowing string:

"There are %s hits, %s are an exact match."

I want %s to be replaced with the numbers that the query returns. In php I would di the开发者_如何学运维 folowing:

$res = sprintf("There are %s hits, %s are an exact match", $number1, $number2);

How do I do this in C#? I thought about string.replace() but that would only work with 1 piece that should be replaced. In this case there are multiple.


You are looking for String.Format:

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

The format specifiers are a bit different than in PHP. Here is an article explaining format specifiers.


You use String.Format.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜